rand和srand怎么用?

       首先说明一下,下面所谓的随机是“伪随机”。 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++;
		else
			even++;
	}
	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++;
		else
			a3++;
	}
	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++;
		else
			a4++;
	}
	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++;
		else
			a5++;
	}
	cout << a1 << endl << a2 << endl << a3 << endl << a4 << endl << a5 << endl;

	return 0;
}

     结果:

 

1922
2034
2043
1997
2004

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

 

 

       

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值