Board logo

標題: 亂數 - 種子亂數 [打印本頁]

作者: 周政輝    時間: 2017-9-2 09:05     標題: 亂數 - 種子亂數

先設定亂數種子, 再利用 rand() 函式產生20組種子亂數
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     // 38 41 45 48 51 54 58
  7.    srand(8);
  8.    for(int i=0;i<20;i++)
  9.    {
  10.     // 2147483647
  11.    
  12.      cout << rand() << endl;        
  13.    }   
  14.    
  15.     system("pause");
  16.     return 0;
  17.    
  18. }
複製代碼

作者: 莊旻叡    時間: 2017-9-2 09:08

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     srand(rand());
  7.     for(int i=0;i<20;i++)
  8.      {
  9.       cout<<rand()<<endl;        
  10.      }
  11.     system("pause");   
  12.     return 0;
  13. }
複製代碼





歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/) Powered by Discuz! 7.2