关键词搜索

源码搜索 ×
×

rand和srand怎么用?

发布2013-04-11浏览8615次

详情内容

       首先说明一下,下面所谓的随机是“伪随机”。 rand()函数的作用是产生[0,RAND_MAX]之间的一个整数,先看rand函数的用法:

 

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int i;
  6. cout << rand() << endl;
  7. cout << rand() << endl;
  8. cout << rand() << endl;
  9. cout << RAND_MAX << endl;
  10. return 0;
  11. }

      结果为:

41
18467
6334
32767

        每次运行上述程序,得到的结果都是一样的。其实随机数是这么产生的,对于一个给定的种子而言,后面的随机数序列都定了(不是真正的随机,是“伪随机”,只是人看起来像是随机数而已),而srand函数就是给定种子的函数,对于上述程序而言,默认的给定种子是1,验证如下:

 

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. srand(1);
  6. cout << rand() << endl;
  7. cout << rand() << endl;
  8. cout << rand() << endl;
  9. srand(0);
  10. cout << rand() << endl;
  11. cout << rand() << endl;
  12. cout << rand() << endl;
  13. return 0;
  14. }

    结果为:

41
18467
6334

38
7719
21238

 

     欣赏程序:

  1. #include<iostream>
  2. #include<ctime>
  3. using namespace std;
  4. int main()
  5. {
  6. srand(time(NULL));
  7. cout << rand() << endl;
  8. cout << rand() << endl;
  9. cout << rand() << endl;
  10. return 0;
  11. }

    结果为:

 

32473
26952
31764


      看看下面程序,结果居然是一样的,想想为什么?

 

  1. #include<iostream>
  2. #include<ctime>
  3. using namespace std;
  4. int getRandom()
  5. {
  6. srand(time(NULL));
  7. return rand();
  8. }
  9. int main()
  10. {
  11. int i;
  12. for(i = 0; i < 10; i++)
  13. {
  14. cout << getRandom() << endl;
  15. }
  16. return 0;
  17. }

      结果都是780(但每次运行的结果都不一致,也就是说不一定是780, 但是,这10个数却是相同的),之所以都是780,是因为,连续10次调用很快,系统时间来不及更换。本人实验过,当把10变成一个较大的数时,所得的结果便不一样了,因为系统时间在走动。
 

     那么,如何让程序以某概率执行某一部分呢?可以参考如下程序:

 

  1. #include<iostream>
  2. #include<ctime>
  3. using namespace std;
  4. int main()
  5. {
  6. int i;
  7. int odd = 0;
  8. int even = 0;
  9. srand(time(NULL));
  10. for(i = 0; i < 10000; i++)
  11. {
  12. if(1 == rand() % 2)
  13. odd++;
  14. else
  15. even++;
  16. }
  17. cout << odd << endl << even << endl;
  18. return 0;
  19. }

     结果为:

 

4976
5024
    

 

  1. #include<iostream>
  2. #include<ctime>
  3. using namespace std;
  4. int main()
  5. {
  6. int i;
  7. int a1 = 0;
  8. int a2 = 0;
  9. int a3 = 0;
  10. int n;
  11. srand(time(NULL));
  12. for(i = 0; i < 10000; i++)
  13. {
  14. n = rand();
  15. if(1 == n % 3)
  16. a1++;
  17. else if(2 == n % 3)
  18. a2++;
  19. else
  20. a3++;
  21. }
  22. cout << a1 << endl << a2 << endl << a3 << endl;
  23. return 0;
  24. }

     结果为:

 

3340
3348
3312
 

 

  1. #include<iostream>
  2. #include<ctime>
  3. using namespace std;
  4. int main()
  5. {
  6. int i;
  7. int a1 = 0;
  8. int a2 = 0;
  9. int a3 = 0;
  10. int a4 = 0;
  11. int n;
  12. srand(time(NULL));
  13. for(i = 0; i < 10000; i++)
  14. {
  15. n = rand();
  16. if(1 == n % 4)
  17. a1++;
  18. else if(2 == n % 4)
  19. a2++;
  20. else if(3 == n % 4)
  21. a3++;
  22. else
  23. a4++;
  24. }
  25. cout << a1 << endl << a2 << endl << a3 << endl << a4 << endl;
  26. return 0;
  27. }

结果为:

 

2522
2502
2500
2476
  

 

  1. #include<iostream>
  2. #include<ctime>
  3. using namespace std;
  4. int main()
  5. {
  6. int i;
  7. int a1 = 0;
  8. int a2 = 0;
  9. int a3 = 0;
  10. int a4 = 0;
  11. int a5 = 0;
  12. int n;
  13. srand(time(NULL));
  14. for(i = 0; i < 10000; i++)
  15. {
  16. n = rand();
  17. if(1 == n % 5)
  18. a1++;
  19. else if(2 == n % 5)
  20. a2++;
  21. else if(3 == n % 5)
  22. a3++;
  23. else if(4 == n % 5)
  24. a4++;
  25. else
  26. a5++;
  27. }
  28. cout << a1 << endl << a2 << endl << a3 << endl << a4 << endl << a5 << endl;
  29. return 0;
  30. }

     结果:

 

1922
2034
2043
1997
2004

      由此看来,对于某一种子而言,循环调用rand函数产生的随机数还真有一点伪随机性,挺好的。

 

 

       

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载