关于随机数 C++

 1 void test()
 2 {
 3     srand(1);//这里设置了 说明又得从头开始循环一次了
 4                     //如果没有设置 它还是基于main函数里的srand(1)
 5     for(int i=0;i<10;i++)
 6     {
 7         cout << rand() << endl;
 8     }
 9 }
10 int main()
11 {
12     srand(1);
13     for(int i=0;i<10;i++)
14     {
15         cout << rand() << endl;
16     }
17 
18     test();
19 }
20 
21 //因此为了使程序每次运行产生的随机数不同,通常使用srand(time(nullptr));
22 // 只要每次运行这个程序的时间间隔大于1秒 就会产生不同的随机数序列
23 // 当然要记住设置一次srand(time(0)) 就够了,如果一个程序内多次设置
24 // 且程序又在一秒内跑完 那么你出来的 随机数 肯定会出现循环的

 

     

 

posted @ 2017-09-27 02:35  Lucky、Dog  阅读(184)  评论(0)    收藏  举报