返回列表 發帖

亂數 - 種子亂數

先設定亂數種子, 再利用 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. }
複製代碼

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

TOP

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

TOP

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

TOP

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

TOP

返回列表