rand和srand怎么用?
来源:互联网 发布:神经网络数据归一化 编辑:程序博客网 时间:2024/04/28 15:36
首先说明一下,下面所谓的随机是“伪随机”。 rand()函数的作用是产生[0,RAND_MAX]之间的一个整数,先看rand函数的用法:
#include<iostream>using namespace std;int main(){int i;cout << rand() << endl;cout << rand() << endl;cout << rand() << endl;cout << RAND_MAX << endl;return 0;}结果为:
41
18467
6334
32767
每次运行上述程序,得到的结果都是一样的。其实随机数是这么产生的,对于一个给定的种子而言,后面的随机数序列都定了(不是真正的随机,是“伪随机”,只是人看起来像是随机数而已),而srand函数就是给定种子的函数,对于上述程序而言,默认的给定种子是1,验证如下:
#include<iostream>using namespace std;int main(){srand(1);cout << rand() << endl;cout << rand() << endl;cout << rand() << endl;srand(0);cout << rand() << endl;cout << rand() << endl;cout << rand() << endl;return 0;}结果为:
41
18467
6334
38
7719
21238
欣赏程序:
#include<iostream>#include<ctime>using namespace std;int main(){srand(time(NULL));cout << rand() << endl;cout << rand() << endl;cout << rand() << endl;return 0;}结果为:
32473
26952
31764
看看下面程序,结果居然是一样的,想想为什么?
#include<iostream>#include<ctime>using namespace std;int getRandom(){srand(time(NULL));return rand();}int main(){int i;for(i = 0; i < 10; i++){cout << getRandom() << endl;}return 0;}结果都是780(但每次运行的结果都不一致,也就是说不一定是780, 但是,这10个数却是相同的),之所以都是780,是因为,连续10次调用很快,系统时间来不及更换。本人实验过,当把10变成一个较大的数时,所得的结果便不一样了,因为系统时间在走动。
那么,如何让程序以某概率执行某一部分呢?可以参考如下程序:
#include<iostream>#include<ctime>using namespace std;int main(){int i;int odd = 0;int even = 0;srand(time(NULL));for(i = 0; i < 10000; i++){if(1 == rand() % 2)odd++;elseeven++;}cout << odd << endl << even << endl;return 0;}结果为:
4976
5024
#include<iostream>#include<ctime>using namespace std;int main(){int i;int a1 = 0;int a2 = 0;int a3 = 0;int n;srand(time(NULL));for(i = 0; i < 10000; i++){n = rand();if(1 == n % 3)a1++;else if(2 == n % 3)a2++;elsea3++;}cout << a1 << endl << a2 << endl << a3 << endl;return 0;}结果为:
3340
3348
3312
#include<iostream>#include<ctime>using namespace std;int main(){int i;int a1 = 0;int a2 = 0;int a3 = 0;int a4 = 0;int n;srand(time(NULL));for(i = 0; i < 10000; i++){n = rand();if(1 == n % 4)a1++;else if(2 == n % 4)a2++;else if(3 == n % 4)a3++;elsea4++;}cout << a1 << endl << a2 << endl << a3 << endl << a4 << endl;return 0;}结果为:
2522
2502
2500
2476
#include<iostream>#include<ctime>using namespace std;int main(){int i;int a1 = 0;int a2 = 0;int a3 = 0;int a4 = 0;int a5 = 0;int n;srand(time(NULL));for(i = 0; i < 10000; i++){n = rand();if(1 == n % 5)a1++;else if(2 == n % 5)a2++;else if(3 == n % 5)a3++;else if(4 == n % 5)a4++;elsea5++;}cout << a1 << endl << a2 << endl << a3 << endl << a4 << endl << a5 << endl;return 0;}结果:
1922
2034
2043
1997
2004
由此看来,对于某一种子而言,循环调用rand函数产生的随机数还真有一点伪随机性,挺好的。
- rand和srand怎么用?
- 用rand()和srand()产生伪随机数
- srand和 rand函数.
- rand()和srand()函数
- rand()和srand()函数
- rand()和srand()函数
- srand()和rand()详解
- rand()和srand()函数
- rand和srand
- rand()和srand()
- srand()和rand()详解
- rand()和srand()函数
- rand 和 srand 详解
- rand和srand
- rand()和srand()
- srand()和rand
- srand() 和 rand()
- rand()和srand()函数
- VS2010下使用QT环境配置
- WARNING L15: MULTIPLE CALL TO SEGMENT错误信息的处理
- 对于public static void main(String args[])你知几何?
- 【露】UK大冒险之——变身小小小黄鸡
- android解析XML文件(采用Pull解析器解析文件)
- rand和srand怎么用?
- 简单java应用程序---比较两个数大小
- 点结构体与类库
- JNDI全攻略(一)
- android编程经验-怎么使文字始终显示最后一行
- Oracle数据库之SQL语句练习篇--准备
- 裁切区域
- Mysql入门学习
- Linux内核设计与实现_读书笔记(一)