• <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 隨機(jī)數(shù)生成

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

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

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

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

            下面是一個(gè)產(chǎn)生100個(gè)偽隨機(jī)數(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í)上我們這個(gè)例子并不是很好,但是它足夠用來(lái)說(shuō)明偽隨機(jī)數(shù)是如何產(chǎn)生的。

             

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

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

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

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

            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)生的偽隨機(jī)數(shù)的范圍在0到RAND_MAX之間,通常它的值在cstdlib中為32767.

            通常我們并不想要這個(gè)范圍內(nèi)的隨機(jī)值。我們想要兩個(gè)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ù)運(yùn)行第2個(gè)程序的時(shí)候,發(fā)現(xiàn)每次的結(jié)果都是相同的。由于我們每次設(shè)置的seed都是相同的。因此我們使用了time()函數(shù)來(lái)產(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: }

             

            怎樣的偽隨機(jī)生成器算是好的呢?

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

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

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

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

             

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

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

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

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

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


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


            <2012年5月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿(1)

            隨筆檔案

            IT網(wǎng)站

            My Friends

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            久久香蕉国产线看观看精品yw| 国产精品熟女福利久久AV| 久久e热在这里只有国产中文精品99| 久久久久亚洲av无码专区导航| 久久精品国产亚洲av麻豆色欲| 久久综合丝袜日本网| 狠狠色伊人久久精品综合网| 久久精品视屏| 久久久精品国产sm调教网站| 国产—久久香蕉国产线看观看| 四虎影视久久久免费| 久久夜色精品国产欧美乱| 国内精品久久久久久麻豆 | 久久国产精品一区| 久久精品国产欧美日韩99热| 九九精品99久久久香蕉| 久久久久久久综合综合狠狠| 久久久噜噜噜久久熟女AA片| 久久精品国产一区二区三区| 久久99国产综合精品| 狠狠色丁香婷婷久久综合| 国产成人久久777777| 久久99精品久久久久婷婷| 91麻豆国产精品91久久久| 久久亚洲2019中文字幕| 久久福利青草精品资源站免费| 中文无码久久精品| 国产69精品久久久久观看软件| 久久这里只有精品久久| 国产日产久久高清欧美一区| 亚洲人成精品久久久久| 久久久精品国产免大香伊| 久久综合色之久久综合| 久久亚洲电影| 久久无码一区二区三区少妇| 中文字幕成人精品久久不卡| 国产精品久久成人影院| 国产精品亚洲综合久久| 伊人色综合久久天天网| 婷婷久久五月天| 久久婷婷五月综合成人D啪 |