溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C++產(chǎn)生隨機(jī)數(shù)的方法有哪些

發(fā)布時(shí)間:2023-03-09 11:44:27 來(lái)源:億速云 閱讀:124 作者:iii 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹“C++產(chǎn)生隨機(jī)數(shù)的方法有哪些”,在日常操作中,相信很多人在C++產(chǎn)生隨機(jī)數(shù)的方法有哪些問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”C++產(chǎn)生隨機(jī)數(shù)的方法有哪些”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

使用cstdlib庫(kù)

C++11之前,C和C++都用相同的方法來(lái)產(chǎn)生隨機(jī)數(shù)(偽隨機(jī)數(shù)),即rand()函數(shù),用法如下:

1)使用srand()撒一個(gè)種子

功能:初始化隨機(jī)數(shù)發(fā)生器

用法:void srand(unsigned int seed)

2)使用rand()產(chǎn)生隨機(jī)數(shù)

功能:隨機(jī)數(shù)發(fā)生器

用法:int rand(void)

3)控制隨機(jī)數(shù)范圍

要取得 [a,b) 的隨機(jī)整數(shù),使用 (rand() % (b-a))+ a;

要取得 [a,b] 的隨機(jī)整數(shù),使用 (rand() % (b-a+1))+ a;

要取得 (a,b] 的隨機(jī)整數(shù),使用 (rand() % (b-a))+ a + 1;

** 參考:C++ rand 與 srand 的用法

4)示例代碼

#include <iostream>
#include <ctime>
#include <cstdlib>
 
int getRand(int min, int max);
 
int main() {
 
    srand(time(0));
 
    for (int i=0; i<10; i++) {
        int r = getRand(2,20);
        std::cout << r << std::endl;
    }
 
    return 0;
}
 
// 左閉右閉區(qū)間
int getRand(int min, int max) {
    return ( rand() % (max - min + 1) ) + min ;
}

使用random庫(kù):c++11 random library

C++11之前,無(wú)論是C,還是C++都使用相同方式的來(lái)生成隨機(jī)數(shù),而在C++11中提供了隨機(jī)數(shù)庫(kù),包括隨機(jī)數(shù)引擎類、隨機(jī)數(shù)分布類,簡(jiǎn)介如下:

隨機(jī)數(shù)引擎類

一般使用 default_random_engine 類,產(chǎn)生隨機(jī)非負(fù)數(shù)(不推薦直接使用)

直接使用時(shí):

#include <iostream>
#include <ctime>
#include <random>
 
int main() {
 
    std::default_random_engine e;
    e.seed(time(0));
 
    for (int i=0; i<10; i++) {
        std::cout << e() << std::endl;
    }
 
    return 0;
}

輸出結(jié)果:

16807
282475249
1622650073
984943658
1144108930
470211272
101027544
1457850878
1458777923
2007237709

隨機(jī)數(shù)分布類

uniform_int_distribution:產(chǎn)生均勻分布的整數(shù)

示例代碼:

#include <iostream>
#include <ctime>
#include <random>
 
int main() {
 
    std::default_random_engine e;
    std::uniform_int_distribution<int> u(2,20); // 左閉右閉區(qū)間
    e.seed(time(0));
 
    for (int i=0; i<10; i++) {
        std::cout << u(e) << std::endl;
    }
 
    return 0;
}

輸出結(jié)果:

4
5
16
17
15
17
6
10
13
13

uniform_real_distribution:產(chǎn)生均勻分布的實(shí)數(shù)
#include <iostream>
#include <ctime>
#include <random>
 
int main() {
 
    std::default_random_engine e;
    std::uniform_real_distribution<double> u(1.5,19.5); // 左閉右閉區(qū)間
    e.seed(time(0));
 
    for (int i=0; i<10; i++) {
        std::cout << u(e) << std::endl;
    }
 
    return 0;
}

輸出結(jié)果:

11.9673
2.29179
9.82668
9.82764
10.2394
13.8324
2.95336
9.72177
16.5145
12.1421

normal_distribution:產(chǎn)生正態(tài)分布的實(shí)數(shù)

示例代碼:

#include <iostream>
#include <ctime>
#include <random>
 
int main() {
 
    std::default_random_engine e;
    std::normal_distribution<double> u(0,1); // 均值為0,標(biāo)準(zhǔn)差為1
    e.seed(time(0));
 
    for (int i=0; i<10; i++) {
        std::cout << u(e) << std::endl;
    }
 
    return 0;
}

輸出結(jié)果:

0.390995
-0.680137
-1.02953
-0.53243
0.375886
-0.19804
-0.796159
0.837714
0.899632
2.06609

bernoulli_distribution:生成二項(xiàng)分布的布爾值
#include <iostream>
#include <ctime>
#include <random>
 
int main() {
 
    std::default_random_engine e;
    std::bernoulli_distribution u(0.8); // 生成1的概率為0.8
    e.seed(time(0));
 
    for (int i=0; i<10; i++) {
        std::cout << u(e) << std::endl;
    }
 
    return 0;
}

輸出結(jié)果:

1
0
1
1
1
1
1
1
1
1

到此,關(guān)于“C++產(chǎn)生隨機(jī)數(shù)的方法有哪些”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

c++
AI