返回列表 發帖

亂數 - 種子亂數

本帖最後由 tonyh 於 2015-4-18 11:11 編輯

先設定亂數種子, 再利用 rand() 函式產生20組種子亂數.
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     srand(5);    //設定亂數種子
  7.     for(int i=1; i<=20; i++)
  8.         cout<<rand()<<endl;    //使用rand()函式產生固定亂數
  9.     system("pause");
  10.     return 0;
  11. }
複製代碼

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

TOP

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

TOP

本帖最後由 李大全 於 2015-4-18 11:43 編輯
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.    
  7.     srand(3);
  8.     for(int a=1;a<=20;a++)
  9.     {
  10.     cout<<rand()<<endl;  
  11.     }
  12.     system("pause");
  13.     return 0;
  14. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     srand(5);
  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()
  7.        for(int a=1 ;a>0; a++)
  8.        cout<<rand()<<",";
  9.         system ("pause") ;
  10.         return 0;
  11. }
複製代碼
http://fs-old.mis.kuas.edu.tw/~s1102137106/music/

TOP

返回列表