一文学会随机函数rand()和srand()的用法

先了解种子seed

  • 随机数种子作用
    (random seed)在伪随机数生成器中用于生成伪随机数的初始数值。对于一个伪随机数生成器,从相同的随机数种子出发,可以得到相同的随机数序列。也就是说只要计算方法一定,随机种子一定,那么产生的随机数就不会变。随机种子来自系统时钟,如当前的时间。

现在了解了关于随机数种子的概念,下面开始了解随机函数rand()srand()的用法与区别吧
先来看通过调用这两个函数产生随机数的两组结果

//rand() Output1: 
On this computer, the RAND_MAX is: 2147483647.
Five numbers the rand function generates as follows:
1804289383
846930886
1681692777
1714636915
1957747793

//rand() Output2: 
On this computer, the RAND_MAX is: 2147483647.
Five numbers the rand function generates as follows:
1804289383
846930886
1681692777
1714636915
1957747793

可以看出通过rand()这个方式生成的随即数其实并不是随机的
//srand() Output1:
On this computer, the RAND_MAX is: 2147483647.
Five numbers the rand function generates as follows:
1160253025
2061887090
750313937
1344018521
551740390

//srand() Output2:
On this computer, the RAND_MAX is: 2147483647.
Five numbers the rand function generates as follows:
332333374
272865263
1858799826
198624662
2079768438

可以看出通过srand()这个方式生成的随机数是随机的

rand()函数

所在头文件:
# include stdlib.h / cstdlib
功能: 随机数发生器
声明:
int rand(void)

无参数,返回值是整型。

rand() 产生的是伪随机数字,它不是真的随机数,每次执行时是相同的。
rand() 返回一随机数值的范围在 0 至 RAND_MAX 间。
RAND_MAX 的范围最少是在 32767 之间(int)。
用 unsigned int 双字节是 65535,四字节是 4294967295 的整数范围。
0~RAND_MAX 每个数字被选中的机率是相同的。
用户未设定随机数种子时,系统默认的随机数种子为 1

上面运行结果的程序代码:

编写程序,调用 rand 函数生成五个随机数(一)
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int i;
cout << "On this computer, the RAND_MAX is: " << RAND_MAX << ".\n";
cout << "Five numbers the rand function generates as follows:\n";
for( i = 0; i < 5; i++ )
cout << rand() << endl;
cout << endl;
return 0;
}

srand()函数

所在头文件:
# include stdlib.h / cstdlib
功能: 随机数发生器
声明:
void srand(unsigned int seed)

参数 seed 是一个整型值,用于伪随机数生成算法播种。无返回值。

srand() 用来设置 rand() 产生随机数时的随机数种子。
参数 seed 必须是个整数,如果每次 seed 都设相同值,rand() 所产生的随机数值每次就会一样。

刚才说种子seed作用时提到随机种子来自系统时钟,如当前的时间。
那么,如何使用当前时钟作为随机数种子?
rand() 产生的随机数在每次运行的时候都是与上一次相同的。若要不同, 只需要用函数 srand() 进行一次初始化。
可以利用 srand((unsigned int)(time(NULL)) 的方法,产生不同的随机数种子,因为每一次运行程序的时间是不同的。

上面运行结果的程序代码:

编写程序,调用 rand 函数生成五个随机数(二)
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int i;
cout << "On this computer, the RAND_MAX is: " << RAND_MAX << ".\n";
cout << "Five numbers the rand function generates as follows:\n";
srand( (int)time(0) );			//调用一次,确保触发随机数的初始值不一样
for( i = 0; i < 5; i++ )
cout << rand() << endl;
cout << "\n";
return 0;
}

产生随机数的步骤

  1. srand()提供一个种子,它是一个unsigned int类型;
  2. 调用rand(),它会根据提供给srand()的种子值返回一个真正的随机数(在0到RAND_MAX之间);
  3. 根据需要多次调用rand(),从而不间断地得到新的随机数;
  4. 无论什么时候,都可以给srand()提供一个新的种子值,从而进一步"随机化"rand()的输出结果。

实例练习

通过下面这两段代码程序 能让你更清楚初始值seed设定的作用

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int i;
time_t t;
unsigned int seed;
cout << "On this computer, the RAND_MAX is: " << RAND_MAX << ".\n";
cout << "Five numbers the rand function generates as follows:\n";

                                              //seed通过time(t)产生,为当前时间,所以每次运行的seed初始值不同
seed = time(&t); //time_t time(time_t *__timer) Return the current time and put it in *TIMER if TIMER is not NULL.
cout << "srand() seed is: "<< seed <<".\n";
cout <<"timer is :" << t << endl;
srand((unsigned)time(&t)); //void srand(unsigned int __seed) Seed the random number generator with the given number.

for( i = 0; i < 5; i++ ){
    cout << rand() << endl;
    cout << "\n";
}

return 0;
}

Outout1
On this computer, the RAND_MAX is: 2147483647.
Five numbers the rand function generates as follows:
srand() seed is: 1624948182.                       ///第一次运行seed是由时间函数返回的当前时间
timer is :1624948182
1603285002
795417619
748917136
703808517
1097324855

Output2
On this computer, the RAND_MAX is: 2147483647.
Five numbers the rand function generates as follows:
srand() seed is: 1624948291.                     ///第二次运行seed是由时间函数返回的当前时间,两次的seed值不一样
timer is :1624948291
1739366377

1922270364

724888365

1545253715

1759853765
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int i;
unsigned int seed;
cout << "On this computer, the RAND_MAX is: " << RAND_MAX << ".\n";
cout << "Five numbers the rand function generates as follows:\n";


seed = 2;                                       ///seed赋值为2
cout << "srand() seed is: "<< seed <<".\n";
srand((unsigned)seed); //void srand(unsigned int __seed) Seed the random number generator with the given number.

for( i = 0; i < 5; i++ ){
    cout << rand() << endl;
    cout << "\n";
}

return 0;


Outout1
On this computer, the RAND_MAX is: 2147483647.
Five numbers the rand function generates as follows:
srand() seed is: 2.                                ///第一次运行seed是人为定义的,值为2
1505335290

1738766719

190686788

260874575

747983061

Output2
On this computer, the RAND_MAX is: 2147483647.
Five numbers the rand function generates as follows:
srand() seed is: 2.                              //第二次运行seed值依然为2,不变
1505335290

1738766719

190686788

260874575

747983061
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kxwang_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值