• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            5.9 隨機數(shù)生成

            from http://www.learncpp.com/cpp-tutorial/59-random-number-generation/

            通常在游戲,統(tǒng)計模型程序和科學(xué)模擬中會用到隨機事件。

            而由于計算機的本質(zhì)結(jié)構(gòu)決定計算機只能生成偽隨機數(shù)據(jù)。

            偽隨機生成器,設(shè)定一個初始值(seed),對它進行操作形成不同的數(shù)值,讓它看上去與初始值沒有聯(lián)系。如果算法足夠復(fù)雜,將同樣的算法用到最后生成的數(shù)字,這樣就能夠產(chǎn)生一些列看上去隨機的數(shù)值。

            下面是一個產(chǎn)生100個偽隨機數(shù)的程序:

            1.

               1: #include <stdafx.h>
               2: #include <iostream>
               3: using namespace std;
               4:  
               5: unsigned int PRNG()
               6: {
               7:     // our initial starting seed is 5323
               8:     static unsigned int nSeed = 5323;
               9:  
              10:     // Take the current seed and generate a new value from it
              11:     // Due to our use of large constants and overflow, it would be
              12:     // very hard for someone to predict what the next number is
              13:     // going to be from the previous one.
              14:     nSeed = (8253729 * nSeed + 2396403);
              15:  
              16:     // Take the seed and return a value between 0 and 32767
              17:     return nSeed  % 32767;
              18: }
              19:  
              20: int main()
              21: {
              22:     // Print 100 random numbers
              23:     for (int nCount=0; nCount < 100; ++nCount)
              24:     {
              25:         cout << PRNG() << "\t";
              26:  
              27:         // If we've printed 5 numbers, start a new column
              28:         if ((nCount+1) % 5 == 0)
              29:             cout << endl;
              30:     }
              31: }

            事實上我們這個例子并不是很好,但是它足夠用來說明偽隨機數(shù)是如何產(chǎn)生的。

             

            C++產(chǎn)生偽隨機數(shù)

            語言中有內(nèi)置的隨機數(shù)生成器,它需要兩個獨立的函數(shù)組合使用。

            srand() 用來設(shè)置初始seed。srand()通常只調(diào)用一次。

            rand()生成序列中的下一個隨機值。

            2.

               1: #include <iostream>
               2: #include <cstdlib> // for rand() and srand()
               3: using namespace std;
               4:  
               5: int main()
               6: {
               7:     srand(5323); // set initial seed value to 5323
               8:  
               9:     // Print 100 random numbers
              10:     for (int nCount=0; nCount < 100; ++nCount)
              11:     {
              12:         cout << rand() <<“\t”;
              13:         
              14:         // If we've printed 5 numbers, start a new column
              15:         if ((nCount+1) % 5 == 0)
              16:         cout << endl;
              17:     }
              18: }

            rand()產(chǎn)生的偽隨機數(shù)的范圍在0到RAND_MAX之間,通常它的值在cstdlib中為32767.

            通常我們并不想要這個范圍內(nèi)的隨機值。我們想要兩個nLow和nHigh范圍內(nèi)。如1-6.

            3.

               1: // Generate a random number between nLow and nHigh (inclusive)
               2: unsigned int GetRandomNumber(int nLow, int nHigh)
               3: {
               4:     return (rand() % (nHigh - nLow + 1)) + nLow;
               5: }

            當(dāng)我們反復(fù)運行第2個程序的時候,發(fā)現(xiàn)每次的結(jié)果都是相同的。由于我們每次設(shè)置的seed都是相同的。因此我們使用了time()函數(shù)來產(chǎn)生seed。

               1: #include <stdafx.h>
               2: #include <iostream>
               3: #include <cstdlib> // for rand() and srand()
               4: #include <ctime> // for time()
               5: using namespace std;
               6:  
               7: int main()
               8: {
               9:  
              10:     srand(time(0)); // set initial seed value to system clock
              11:     for (int nCount=0; nCount < 100; ++nCount)
              12:     {
              13:         cout << rand() << "\t";
              14:  
              15:         if ((nCount+1) % 5 == 0)
              16:             cout << endl;
              17:     }
              18: }

             

            怎樣的偽隨機生成器算是好的呢?

            1)生成器會以相同的概率產(chǎn)生每一個數(shù)值。

            2)隨機序列的下一個數(shù)字不能明顯、容易被預(yù)測

            3)隨機數(shù)生成器產(chǎn)生的數(shù)值有一個好的分布,忽而小,忽而大,讓人感覺是隨機的。

            4)所有的偽隨機生成器都是周期性的

             

            rand()函數(shù)只是一個普通的偽隨機生成器

            大多數(shù)rand()都是使用Linear Congruential Generator方法實現(xiàn)的。

            由于RAND_MAX通常是32767,意味著如果我們想要更大的范圍就會不適合了。同時,rand()也不適合產(chǎn)生浮點型隨機值如0.0-1.0. 同時rand()相對于其他算法擁有一個短的周期。

            可以使用Mersenne Twister,它具有更好的結(jié)果,同時使用相對簡單。

            posted on 2012-05-21 20:50 鐘謝偉 閱讀(1256) 評論(0)  編輯 收藏 引用


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            <2012年6月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            1234567

            導(dǎo)航

            統(tǒng)計

            常用鏈接

            留言簿(1)

            隨筆檔案

            IT網(wǎng)站

            My Friends

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            国产高清国内精品福利99久久| 久久人与动人物a级毛片| 亚洲精品无码专区久久同性男| 国产精品对白刺激久久久| 国产精品国色综合久久| 婷婷久久久亚洲欧洲日产国码AV | 精品国产91久久久久久久a| 99久久久国产精品免费无卡顿| 亚洲AV无码久久精品成人 | 久久天天躁狠狠躁夜夜网站| 一日本道伊人久久综合影| 久久精品综合网| 日产精品久久久久久久| 亚洲狠狠婷婷综合久久蜜芽| 色综合久久久久综合体桃花网 | 久久99精品国产99久久| 精品久久久久久中文字幕| 久久免费高清视频| 久久久久久av无码免费看大片| 蜜臀久久99精品久久久久久| 久久婷婷国产剧情内射白浆 | 久久精品视屏| 亚洲午夜无码久久久久| 国产精品久久久久久| 亚洲国产天堂久久综合网站| 久久精品亚洲乱码伦伦中文| 久久人做人爽一区二区三区| 69久久夜色精品国产69| 国产福利电影一区二区三区久久老子无码午夜伦不| 亚洲国产精品热久久| 香蕉久久久久久狠狠色| 精品久久久久久国产| 久久免费大片| 国产精品视频久久久| 国内精品伊人久久久影院| 久久国产精品99久久久久久老狼| 国产精品日韩欧美久久综合| 性欧美丰满熟妇XXXX性久久久 | 人妻久久久一区二区三区| 国产精品成人久久久久久久| 久久国产免费直播|