返回列表 發帖

亂數 - 隨機亂數

  1. //加入time(NULL)函式使亂數種子的值隨時間而改變
  2. #include<iostream>
  3. #include<cstdlib>
  4. using namespace std;
  5. int main()
  6. {
  7.     long t=time(NULL);
  8.     cout<<"從1970年1月1日0時0分0秒到現在經過了: "<<t<<"秒"<<endl;
  9.     cout<<"(約"<<t/(365*24*60*60)<<"年)"<<endl;
  10.     srand(t);  //設定亂數種子
  11.     //srand(time(NULL));
  12.     for(int i=1; i<=10; i++)
  13.     {
  14.         cout<<rand()<<endl;  //依據亂數種子的值產生固定亂數
  15.     }
  16.     system("pause");
  17.     return 0;   
  18. }
複製代碼

返回列表