C++中生成隨機數(shù)的方法總結(jié)
背景
C++ 11 在頭文件 #include 中定義了隨機數(shù)庫,也可以使用 C 中生成隨機數(shù)的方法。
C 生成隨機數(shù)
概述
C 語言中使用 rand() 函數(shù)產(chǎn)生 0 ~ RAND_MAX 范圍內(nèi)均勻分布到整數(shù),其中 RAND_MAX 是和系統(tǒng)相關(guān)的一個固定值。
#include<stdlib.h> #include<time.h> srand(time(nullptr));//設(shè)置隨機數(shù)種子 rand();//產(chǎn)生一個隨機數(shù)
限定隨機數(shù)范圍
{//產(chǎn)生 [0,b) 范圍內(nèi)到隨機數(shù) int randoxNumber = rand() % b ; } {//產(chǎn)生 [a,b) 范圍內(nèi)到隨機數(shù) int randoxNumber = a + rand() % ( b -a ) ; } {//產(chǎn)生 [a,b] 范圍內(nèi)到隨機數(shù) int randoxNumber = a + rand() % ( b -a +1 ) ; } {//產(chǎn)生 [0,1] 范圍內(nèi)到隨機小數(shù) double randoxNumber =rand() / RAND_MAX } {//產(chǎn)生 [0,1) 范圍內(nèi)到隨機小數(shù) double randoxNumber =rand() / ( RAND_MAX +1 ) }
C++ 中的隨機數(shù)
概述
C++ 11 在頭文件 #include 中定義了隨機數(shù)庫,包括隨機數(shù)生成器和隨機數(shù)分布器。
隨機數(shù)生成器
①.概述
隨機數(shù)生成器用來使用指定的種子產(chǎn)生一個隨機數(shù)。
②.random_device
random_device 是標準庫提供到一個非確定性隨機數(shù)生成器,使用硬件作為隨機數(shù)來源,故其調(diào)用代價較高,一般用來產(chǎn)生隨機數(shù)種子。
random_device rd; for (int i = 0; i < 10; ++i) { cout << rd() << endl; }
③.default_random_engine
default_random_engine 是標準庫提供的默認隨機數(shù)生成器,其實現(xiàn)和編譯器有關(guān)。
random_device rd; default_random_engine r_eng(rd()); for (int i = 0; i < 10; ++i) { cout << r_eng() << endl; }
④.minstd_rand
minstd_rand 是標準庫提供的采用線性同余算法的偽隨機數(shù)生成器。
random_device rd; minstd_rand r_eng(rd()); for (int i = 0; i < 10; ++i) { cout << r_eng() << endl; }
⑤.mt19937
mt19937 是標準庫提供的采用梅森旋轉(zhuǎn)算法的偽隨機數(shù)生成器,可以快速產(chǎn)生高質(zhì)量到隨機數(shù)。
random_device rd; mt19937 r_eng(rd()); for (int i = 0; i < 10; ++i) { cout << r_eng() << endl; }
⑥.ranlux24_base
ranlux24_base 是標準庫提供的采用帶進位減法的偽隨機數(shù)生成器。
random_device rd; ranlux24_base r_eng(rd()); for (int i = 0; i < 10; ++i) { cout << r_eng() << endl; }
隨機數(shù)分布器
①.概述
隨機數(shù)分布器用于限定生成隨機數(shù)的范圍及分布類型。
②.uniform_int_distribution
uniform_int_distribution 用于生成指定范圍的均勻分布的整數(shù)。
random_device rd;//用于生成隨機數(shù)種子 mt19937 r_eng(rd());//隨機數(shù)生成器 uniform_int_distribution<int> dis(1, 100);//隨機數(shù)分布器 閉區(qū)間 for (int i = 0; i < 10; ++i) { cout << dis(r_eng) << endl; }
③.uniform_real_distribution
uniform_real_distribution 用于生成指定范圍的均勻分布的浮點數(shù)。
random_device rd;//用于生成隨機數(shù)種子 mt19937 r_eng(rd());//隨機數(shù)生成器 uniform_real_distribution<double> dis(1, 100);//隨機數(shù)分布器 閉區(qū)間 for (int i = 0; i < 10; ++i) { cout << dis(r_eng) << endl; }
④.normal_distribution
normal_distribution 用于生成指定均值和方差的正態(tài)分布的浮點數(shù)。
random_device rd;//用于生成隨機數(shù)種子 mt19937 r_eng(rd());//隨機數(shù)生成器 normal_distribution <> dis(4, 1.5);//隨機數(shù)分布器,均值、方差 for (int i = 0; i < 10; ++i) { cout << dis(r_eng) << endl; }
⑤.bernoulli_distribution
bernoulli_distribution 用于生成二項分布到布爾值,可以指定 true 的概率。
random_device rd;//用于生成隨機數(shù)種子 mt19937 r_eng(rd());//隨機數(shù)生成器 bernoulli_distribution dis( 0.6);//隨機數(shù)分布器,生成 1 的概率是 0.6 for (int i = 0; i < 10; ++i) { cout << dis(r_eng) << endl; }
Qt 中的隨機數(shù)
概述
Qt 中生成隨機數(shù)的方法和 C 語言中差不多,對應到函數(shù)為 qsrand() 、qrand()。使用使需要包含頭文件 #include 。
代碼示例
auto seed = QDateTime::currentDateTime().toMSecsSinceEpoch(); qsrand(seed); for (int i = 0; i < 10; ++i) { qDebug() << qrand() % 10;// 0 - 9 范圍 }
以上就是C++中生成隨機數(shù)的方法總結(jié)的詳細內(nèi)容,更多關(guān)于C++生成隨機數(shù)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C語言數(shù)據(jù)結(jié)構(gòu)之循環(huán)鏈表的簡單實例
這篇文章主要介紹了C語言數(shù)據(jù)結(jié)構(gòu)之循環(huán)鏈表的簡單實例的相關(guān)資料,需要的朋友可以參考下2017-06-06使用Objective-C獲取IPHONE手機IMSI序列號
這篇文章主要介紹了使用Objective-C獲取IPHONE手機IMSI序列號的方法以及通過IMSI序列號獲取運營商、手機號的方法,非常的實用,有需要的小伙伴可以參考下。2015-04-04C++ Log日志類輕量級支持格式化輸出變量實現(xiàn)代碼
這篇文章主要介紹了C++ Log日志類輕量級支持格式化輸出變量實現(xiàn)代碼,需要的朋友可以參考下2019-04-04