關於隨機數 C++

來源:互聯網
上載者:User

標籤:函數   ptr   and   main   運行   nbsp   隨機   main函數   logs   

 1 void test() 2 { 3     srand(1);//這裡設定了 說明又得從頭開始迴圈一次了 4                     //如果沒有設定 它還是基於main函數裡的srand(1) 5     for(int i=0;i<10;i++) 6     { 7         cout << rand() << endl; 8     } 9 }10 int main()11 {12     srand(1);13     for(int i=0;i<10;i++)14     {15         cout << rand() << endl;16     }17 18     test();19 }20 21 //因此為了使程式每次運行產生的隨機數不同,通常使用srand(time(nullptr));22 // 只要每次運行這個程式的時間間隔大於1秒 就會產生不同的隨機數序列23 // 當然要記住設定一次srand(time(0)) 就夠了,如果一個程式內多次設定24 // 且程式又在一秒內跑完 那麼你出來的 隨機數 肯定會出現迴圈的

 

     

 

關於隨機數 C++

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.