C++中生成隨機數的方法總結
背景
C++ 11 在頭文件 #include 中定義了隨機數庫,也可以使用 C 中生成隨機數的方法。
C 生成隨機數
概述
C 語言中使用 rand() 函數產生 0 ~ RAND_MAX 范圍內均勻分布到整數,其中 RAND_MAX 是和系統(tǒng)相關的一個固定值。
#include<stdlib.h> #include<time.h> srand(time(nullptr));//設置隨機數種子 rand();//產生一個隨機數
限定隨機數范圍
{//產生 [0,b) 范圍內到隨機數
int randoxNumber = rand() % b ;
}
{//產生 [a,b) 范圍內到隨機數
int randoxNumber = a + rand() % ( b -a ) ;
}
{//產生 [a,b] 范圍內到隨機數
int randoxNumber = a + rand() % ( b -a +1 ) ;
}
{//產生 [0,1] 范圍內到隨機小數
double randoxNumber =rand() / RAND_MAX
}
{//產生 [0,1) 范圍內到隨機小數
double randoxNumber =rand() / ( RAND_MAX +1 )
}
C++ 中的隨機數
概述
C++ 11 在頭文件 #include 中定義了隨機數庫,包括隨機數生成器和隨機數分布器。
隨機數生成器
①.概述
隨機數生成器用來使用指定的種子產生一個隨機數。
②.random_device
random_device 是標準庫提供到一個非確定性隨機數生成器,使用硬件作為隨機數來源,故其調用代價較高,一般用來產生隨機數種子。
random_device rd;
for (int i = 0; i < 10; ++i)
{
cout << rd() << endl;
}

③.default_random_engine
default_random_engine 是標準庫提供的默認隨機數生成器,其實現(xià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 是標準庫提供的采用線性同余算法的偽隨機數生成器。
random_device rd;
minstd_rand r_eng(rd());
for (int i = 0; i < 10; ++i)
{
cout << r_eng() << endl;
}
⑤.mt19937
mt19937 是標準庫提供的采用梅森旋轉算法的偽隨機數生成器,可以快速產生高質量到隨機數。
random_device rd;
mt19937 r_eng(rd());
for (int i = 0; i < 10; ++i)
{
cout << r_eng() << endl;
}
⑥.ranlux24_base
ranlux24_base 是標準庫提供的采用帶進位減法的偽隨機數生成器。
random_device rd;
ranlux24_base r_eng(rd());
for (int i = 0; i < 10; ++i)
{
cout << r_eng() << endl;
}
隨機數分布器
①.概述
隨機數分布器用于限定生成隨機數的范圍及分布類型。
②.uniform_int_distribution
uniform_int_distribution 用于生成指定范圍的均勻分布的整數。
random_device rd;//用于生成隨機數種子
mt19937 r_eng(rd());//隨機數生成器
uniform_int_distribution<int> dis(1, 100);//隨機數分布器 閉區(qū)間
for (int i = 0; i < 10; ++i)
{
cout << dis(r_eng) << endl;
}

③.uniform_real_distribution
uniform_real_distribution 用于生成指定范圍的均勻分布的浮點數。
random_device rd;//用于生成隨機數種子
mt19937 r_eng(rd());//隨機數生成器
uniform_real_distribution<double> dis(1, 100);//隨機數分布器 閉區(qū)間
for (int i = 0; i < 10; ++i)
{
cout << dis(r_eng) << endl;
}

④.normal_distribution
normal_distribution 用于生成指定均值和方差的正態(tài)分布的浮點數。
random_device rd;//用于生成隨機數種子
mt19937 r_eng(rd());//隨機數生成器
normal_distribution <> dis(4, 1.5);//隨機數分布器,均值、方差
for (int i = 0; i < 10; ++i)
{
cout << dis(r_eng) << endl;
}

⑤.bernoulli_distribution
bernoulli_distribution 用于生成二項分布到布爾值,可以指定 true 的概率。
random_device rd;//用于生成隨機數種子
mt19937 r_eng(rd());//隨機數生成器
bernoulli_distribution dis( 0.6);//隨機數分布器,生成 1 的概率是 0.6
for (int i = 0; i < 10; ++i)
{
cout << dis(r_eng) << endl;
}

Qt 中的隨機數
概述
Qt 中生成隨機數的方法和 C 語言中差不多,對應到函數為 qsrand() 、qrand()。使用使需要包含頭文件 #include 。
代碼示例
auto seed = QDateTime::currentDateTime().toMSecsSinceEpoch();
qsrand(seed);
for (int i = 0; i < 10; ++i)
{
qDebug() << qrand() % 10;// 0 - 9 范圍
}
以上就是C++中生成隨機數的方法總結的詳細內容,更多關于C++生成隨機數的資料請關注腳本之家其它相關文章!
相關文章
C++實現(xiàn)LeetCode(102.二叉樹層序遍歷)
這篇文章主要介紹了C++實現(xiàn)LeetCode(102.二叉樹層序遍歷),本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下2021-07-07
IOS開發(fā)之UIScrollView實現(xiàn)圖片輪播器的無限滾動
這篇文章主要介紹了IOS開發(fā)之UIScrollView實現(xiàn)圖片輪播器的無限滾動的相關資料,需要的朋友可以參考下2017-07-07

