<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 1,  comments - 0,  trackbacks - 0
    隨機(jī)數(shù)與隨機(jī)種子      
          調(diào)用隨機(jī)數(shù)函數(shù) rand() 的時(shí)候, 實(shí)際得到的這個(gè)隨機(jī)數(shù)并不是絕對(duì)隨機(jī)的,它是以一個(gè)初始值,通過(guò)一個(gè)算法,計(jì)算出來(lái)的“偽隨機(jī)數(shù)"數(shù)列,每次調(diào)用rand()時(shí),從這個(gè)數(shù)列依次取出一個(gè)值,做為隨機(jī)數(shù)。這個(gè)初始的值就是"隨機(jī)數(shù)種子", 也就是說(shuō),如果隨機(jī)數(shù)種子相同,計(jì)算出的隨機(jī)數(shù)數(shù)列是相同的。而srandom( x) 這個(gè)函數(shù)就是初始化隨機(jī)數(shù)產(chǎn)生器,設(shè)定隨機(jī)數(shù)種子用的。給定的x的就是隨機(jī)數(shù)種子。可以驗(yàn)證,當(dāng)你多次調(diào)用srandm(x)時(shí),如果x取值相同,則得到的隨機(jī)數(shù)數(shù)列是一樣的。所以,若我們每次運(yùn)行程序時(shí),要得到不同的隨機(jī)數(shù)序列,就應(yīng)該用不同的種子來(lái)初始化這個(gè)隨機(jī)數(shù)產(chǎn)生器。比如說(shuō),用時(shí)間初始化它,或者用getpid(),用進(jìn)程的pid號(hào)初始化,由于每次運(yùn)行程序時(shí),它的pid號(hào)一般是不同的,所以能夠產(chǎn)生不同的隨機(jī)數(shù)序列。

    舉例說(shuō)明
    在vc++中程序中用了srandom()和random(),頭文件為stdlib.h,但編譯出現(xiàn)錯(cuò)誤error C3861: “srandom”: 找不到標(biāo)識(shí)符。
      原因是現(xiàn)在vc++編譯器的庫(kù)函數(shù)中沒(méi)有randomize()和random(),分別用srand()和rand()代替了。
      #include <time.h> //定義關(guān)于時(shí)間的函數(shù)  
      一般在用到time(NULL)(當(dāng)前時(shí)間)函數(shù)時(shí)需要包含此頭文件  
      #include <stdlib.h> //定義雜項(xiàng)函數(shù)及內(nèi)存分配函數(shù)  
      一般在用到rand()和srand()函數(shù)時(shí)需要包含此頭文件  

      函數(shù)名: random 功 能: 隨機(jī)數(shù)發(fā)生器,也就是產(chǎn)生一個(gè)隨機(jī)數(shù)  
      用 法: int random(int num);  
      產(chǎn)生的隨機(jī)數(shù)范圍為0~num-1。  

      函數(shù)名: randomize  
      功 能: 初始化隨機(jī)數(shù)發(fā)生器,相當(dāng)于撥隨機(jī)種子  
      用 法: void randomize(void);
     
    1. #include <iostream>  
    2. #include <stdlib.h>    // Need random(), srandom()  
    3. #include <time.h>      // Need time()  
    4. #include <algorithm>   // Need sort(), copy()  
    5. #include <vector>      // Need vector  
    6.   
    7. using namespace std;  
    8.   
    9. void Display(vector<int>& v, const char* s);  
    10.   
    11. int main()  
    12. {  
    13.     // Seed the random number generator  
    14.     srand(time(NULL));  
    15.     // Construct vector and fill with random integer values  
    16.     vector<int> collection(10);  
    17.     for (int i = 0; i < 10; i++)  
    18.         collection[i] = rand() % 10000;  
    19.       
    20.     // Display, sort, and redisplay  
    21.     Display(collection, "Before sorting");  
    22.     sort(collection.begin(), collection.end());  
    23.     Display(collection, "After sorting");  
    24.     return 0;  
    25. }  
    26.   
    27. // Display label s and contents of integer vector v  
    28. void Display(vector<int>& v, const char* s)  
    29. {  
    30.     cout << endl << s << endl;  
    31.     copy(v.begin(), v.end(),ostream_iterator<int>(cout, "\t"));  
    32.     cout << endl;  
    33. }  
    一般情況下可以設(shè)置當(dāng)前時(shí)間為種子 srandom((int)time(0));
    需要多次取隨即數(shù)時(shí),可以將上次拿到的隨即數(shù)作為種子

    posted on 2012-03-29 15:15 憤怒的考拉 閱讀(1212) 評(píng)論(0)  編輯  收藏

    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    常用鏈接

    留言簿

    隨筆檔案

    文章檔案

    搜索

    •  

    最新評(píng)論

    主站蜘蛛池模板: 91精品国产免费网站| A片在线免费观看| 在线免费一区二区| 亚洲精品国产综合久久久久紧| 97在线观免费视频观看| 亚洲中文字幕无码爆乳app| 天天看片天天爽_免费播放| 亚洲综合色一区二区三区| 国产一精品一AV一免费孕妇| 国产成人免费高清激情明星| 亚洲成a人片毛片在线| 我们的2018在线观看免费高清| 亚洲综合伊人久久大杳蕉| 亚洲国产成人精品无码区二本| 韩国欧洲一级毛片免费| 黄网站在线播放视频免费观看| 久久99九九国产免费看小说| 亚洲另类无码专区丝袜| 免费看男女下面日出水视频| 国产免费久久久久久无码| 亚洲AV无码久久精品狠狠爱浪潮 | 亚洲免费视频播放| 亚洲av午夜精品无码专区| 暖暖免费高清日本中文| 免费在线人人电影网| 亚洲国产精彩中文乱码AV| 丰满少妇作爱视频免费观看| 久久精品国产亚洲麻豆| 7x7x7x免费在线观看| 亚洲美国产亚洲AV| 亚洲麻豆精品国偷自产在线91| 日本免费人成网ww555在线| 亚洲精品国产日韩| 亚色九九九全国免费视频| 亚洲AV日韩AV一区二区三曲| 亚洲精品国产精品乱码在线观看| 18禁止看的免费污网站| 美女裸免费观看网站| 亚洲精品国产第1页| 亚洲国产综合人成综合网站| 亚欧免费视频一区二区三区|