Board logo

標題: 亂數 - 隨機亂數 [打印本頁]

作者: 周政輝    時間: 2017-9-2 09:43     標題: 亂數 - 隨機亂數

搭配時間函式, 產生20組隨機亂數.
time(NULL)可回傳, 從格林威治時間 1970年1月1號 0時0分0秒開始到現在的秒數.
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<time.h>
  4. using namespace std;
  5. int main()
  6. {
  7.     // 38 41 45 48 51 54 58 61 64 68
  8.    srand(time(NULL));
  9.    for(int i=0;i<20;i++)
  10.    {
  11.     // 2147483647
  12.    
  13.      cout << rand() << endl;        
  14.    }   
  15.    
  16.     system("pause");
  17.     return 0;
  18.    
  19. }
複製代碼

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

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





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