• <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 隨機數生成

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

            通常在游戲,統計模型程序和科學模擬中會用到隨機事件。

            而由于計算機的本質結構決定計算機只能生成偽隨機數據。

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

            下面是一個產生100個偽隨機數的程序:

            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: }

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

             

            C++產生偽隨機數

            語言中有內置的隨機數生成器,它需要兩個獨立的函數組合使用。

            srand() 用來設置初始seed。srand()通常只調用一次。

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

            通常我們并不想要這個范圍內的隨機值。我們想要兩個nLow和nHigh范圍內。如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: }

            當我們反復運行第2個程序的時候,發現每次的結果都是相同的。由于我們每次設置的seed都是相同的。因此我們使用了time()函數來產生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)生成器會以相同的概率產生每一個數值。

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

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

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

             

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

            大多數rand()都是使用Linear Congruential Generator方法實現的。

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

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

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

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

            導航

            統計

            常用鏈接

            留言簿(1)

            隨筆檔案

            IT網站

            My Friends

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久婷婷激情综合色综合俺也去| 国産精品久久久久久久| 久久国语露脸国产精品电影| 伊人色综合久久天天人手人婷| 久久久久久久97| 久久九色综合九色99伊人| 婷婷国产天堂久久综合五月| 伊人久久大香线蕉av不卡| 狠狠色丁香婷婷久久综合不卡| 精品国产青草久久久久福利| 久久久久亚洲AV成人网人人网站 | 国产成人久久精品一区二区三区| 色成年激情久久综合| 久久久亚洲欧洲日产国码是AV| 国产成人精品白浆久久69| 欧美麻豆久久久久久中文| 久久综合给久久狠狠97色| 思思久久99热免费精品6| 国产亚洲精品自在久久| 国内精品久久久久影院亚洲| 亚洲欧美日韩精品久久| 久久综合给合久久国产免费| 久久亚洲高清综合| 精品久久人人做人人爽综合| 日本久久久久亚洲中字幕| 亚洲国产日韩综合久久精品| www亚洲欲色成人久久精品| 久久久久久久久久久久中文字幕 | 久久精品天天中文字幕人妻| 久久久国产视频| 久久精品国产精品亚洲艾草网美妙| 久久这里只有精品18| 亚洲中文字幕无码久久2020| 亚洲国产成人久久综合碰| 久久国产精品一区| 久久黄视频| 久久久久一级精品亚洲国产成人综合AV区 | 2020最新久久久视精品爱| 国内精品久久久久影院日本| 久久亚洲日韩精品一区二区三区| 亚洲中文久久精品无码ww16|