35 - rand()函数

1 函数原型

rand():生成随机数,函数原型如下:

int rand (void);

cstdlib库描述如下:

Generate random number
1. Returns a pseudo-random integral number in the range between 0 and RAND_MAX.
2. This number is generated by an algorithm that returns a sequence of apparently non-related numbers each time it is called. 
3. This algorithm uses a seed to generate the series, which should be initialized to some distinctive value using function srand.
4. RAND_MAX is a constant defined in <cstdlib>.
5. A typical way to generate trivial pseudo-random numbers in a determined range using rand is to use the modulo of the returned value by the range span and add the initial value of the range :
(1) v1 = rand() % 100;         // v1 in the range 0 to 99
(2) v2 = rand() % 100 + 1;     // v2 in the range 1 to 100
(3) v3 = rand() % 30 + 1985;   // v3 in the range 1985-2014 
6. Notice though that this modulo operation does not generate uniformly distributed random numbers in the span (since in most cases this operation makes lower numbers slightly more likely).
  1. rand()函数:
    (1)用于生成一个伪随机整数;
  2. rand()函数生成的随机数序列是由其内部的随机数生成算法以及该算法被初始化时所使用的种子(seed)共同决定的:
    (1)种子为算法提供初始状态,算法基于种子生成随机数序列;
    (2)相同的种子生成相同的随机数序列,不同的种子生成不同的随机数序列;
    (3)算法是确定的,种子是可设置的;
    (4)rand()函数生成的随机数介于0和RAND_MAX之间;RAND_MAX是一个常量,通常是32767;
    (5)rand()函数生成的随机数分布是均匀的,即在给定的范围内,每个数被选中的概率大致相等。

2 参数

rand()函数的参数为void。

3 返回值

rand()函数的返回值类型为int型:

  1. 返回一个介于0到RAND_MAX之间的整数。

cstdlib库描述如下:

1. An integer value between 0 and RAND_MAX.

4 使用方法

4.1 使用srand()函数提供种子

在使用rand()函数之前,通常需要先调用srand()函数来为rand()函数提供种子;srand()函数原型如下:

void srand (unsigned int seed);

cstdlib库描述如下:

Initialize random number generator
1. The pseudo-random number generator is initialized using the argument passed as seed.
2. For every different seed value used in a call to srand, the pseudo-random number generator can be expected to generate a different succession of results in the subsequent calls to rand.
3. Two different initializations with the same seed will generate the same succession of results in subsequent calls to rand.
4. If seed is set to 1, the generator is reinitialized to its initial value and produces the same values as before any call to rand or srand.
5. In order to generate random-like numbers, srand is usually initialized to some distinctive runtime value, like the value returned by function time (declared in header <ctime>). This is distinctive enough for most trivial randomization needs.
  1. srand()函数的使用方法:
    (1)srand()函数通常只被调用一次;
    (2)srand()函数通常在程序的开始处调用;
    (3)srand()函数通常使用time()函数的返回值作为参数;
    (4)由于每次程序运行时的当前时间是不同的,因此srand()函数提供的种子是不同的,从而rand()函数生成随机数序列也是不同的。

4.2 生成指定范围内的随机数

虽然rand()函数生成的随机数介于0到RAND_MAX之间,但你可以通过一些简单的数学运算来生成指定范围内的随机数。例如,要生成一个介于a和b(包括a和b)之间的随机数,可以这样做:

int min = a;  
int max = b;  
int randNum = min + rand() % (max - min + 1);

这里的%是取模运算符,用于限制rand()函数生成数的范围。

特别注意 模运算可能会导致生成的随机数在指定范围内不是完全均匀分布的;特别地,当RAND_MAX不是目标范围的整数倍时,这种方法会使范围内的某些数(尤其是低数值)的出现概率略高于其他数。

5 示例

示例代码如下所示:

int main() {
   //
   int random_number = 0;
   // 设置随机数种子
   srand((unsigned)time(0));
   // 生成并打印 10 个随机数
   for (int i = 0; i < 10; i++) {
      // 生成随机数
      random_number = rand();
      printf("Random Number %2d: %d\n", i + 1, random_number);
   }
   //
   return 0;
}

代码运行结果如下图所示:

  1. 第1次运行代码
    在这里插入图片描述
  2. 第2次运行代码
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值