C++随机数(rand和srand)函数用法详解(Detailed explanation of C + + random number (Rand and srand) function usage)

C++ 提供了一组函数以生成和使用随机数字。随机数字就是从一组可能的值中进行随机选择而获得的一个值。该组中的值都有相同的被选中的几率。随机数字常用于许多不同类型的程序中,以下是一些示例:

  • 计算机游戏通常要使用随机数字来模拟一些随机过程,例如掷骰子或发牌。
  • 模拟程序使用随机数字来决定后续将要发生的一系列操作或人与动物等的行为。可以创建使用随机数字的公式以确定特定事件在程序中出现的时间。
  • 数据分析程序可能会使用随机数字随机选择要检验的数据。
  • 计算机安全系统使用随机数字来加密敏感数据。

C++ 库有一个名为 rand() 的函数,每次调用该函数都将返回一个非负整数。要使用 rand() 函数,必须在程序中包含 <cstdlib> 头文件。以下是其用法示例:

randomNum = rand();

但是,该函数返回的数字其实是伪随机数。这意味着它们具有随机数的表现和属性,但实际上并不是随机的,它们实际上是用算法生成的。该算法需要一个起始值,称为种子,以生成数字。如果没有给出一个种子,那么它将在每次运行时产生相同的数字流。下面的程序说明了这一点:

  • //This program demonstrates what happens in C++ if you
  • // try to generate random numbers without setting a “seed”.
  • #include
  • #include // Header file needed to use rand
  • using namespace std;
  • int main()
  • {
  • // Generate and printthree random numbers
  • cout << rand() << " ";
  • cout << rand() << " ";
  • cout << rand() << endl ;
  • return 0;
  • }

第1次运行输出结果:41 18467 : 6334第2次运行输出结果:41 18467 6334

要在每次运行程序时获得不同的随机数字流,则必须为随机数生成器提供一个种子以开始。在 C++ 中,这是通过调用 srand 函数完成的。在 rand 被调用之前,srand 函数要先被调用,并且 srand 在整个程序中仅被调用一次。

  • // This program demonstrates using random numbers when a
  • // “seed” is provided for the random number generator.
  • #include
  • #include // Header file needed to use srand and rand
  • using namespace std;
  • int main()
  • {
  • unsigned seed; // Random generator seed
  • // Get a nseed” value from the user
  • cout << "Enter a seed value: ";
  • cin >> seed;
  • // Set the random generator seed before calling rand()
  • srand(seed);
  • //Now generate and print three random numbers
  • cout << rand() << " ";
  • cout << rand() << " ";
  • cout << rand() << endl;
  • return 0;
  • }

第1次运行结果:Enter a seed value: 19100 15331 – 209第2次运行结果:Enter a seed value: 171597 10689 28587

程序中,第 9 行中创建的用于保存种子的变量 seed 被声明为 unsigned 无符号类型。这个数据类型只保存非负整数。这是 srand 函数在调用时期望接收的数据类型,因此使用 unsigned 变量类型可以保证不会将负数发送给 srand。从程序的输出可以看出,每次程序使用不同的种子运行时,都会生成不同的随机数字流。然而,如果再次使用 19 或 171 作为种子运行程序,则将获得与第一次完全相同的数字。程序的第 12 行中,使用 cin 从用户的输入获取随机数生成器种子的值。实际上,获取种子值的另一个常见做法是调用 time 函数,它是 C++ 标准库的一部分。time 函数返回从 1970 年 1 月 1 日午夜开始到现在逝去的秒数,因此每次运行程序时,它都将提供不同的种子值。下面程序演示了 time 函数的用法。请注意,在调用它时必须给它传递一个参数 0。同时程序中包含一个新的头文件 ctime,此头文件是使用 time 函数所必需的。

  • //This program demonstrates using the C++ time function
  • //to provide a nseed,T for the random number generator.
  • #include
  • #include // Header file needed to use srand and rand
  • #include // Header file needed to use time
  • using namespace std;
  • int main()
  • {
  • unsigned seed; // Random generator seed
  • // Use the time function to get a “seed” value for srand
  • seed = time(0);
  • srand(seed);
  • // Now generate and print three random numbers
  • cout << rand() << " " ;
  • cout << rand() << " " ;
  • cout << rand() << endl;
  • return 0;
  • }

程序输出结果:

2961 21716 181

限制随机数的范围

有时程序需要一个特定范围内的随机数。要将随机数的范围限制在 1 和某个最大值 max 之间的整数,可以使用以下公式:

number = rand() % max + 1;

例如,要生成 16 的随机数来代表骰子的点数,则可以使用以下语句:

dice = rand() % 6 + 1;

这里简单介绍一下其工作原理。求余数运算符(%)可以获得整除之后的余数。当使用通过 rand 函数返回的正整数除以6时,余数将是 05 的数字。因为目标是 16 的数字,所以只需要给余数加 1 即可。这个想法可以扩展到任意范围内的随机数,其通用公式如下:

number = (rand()%(maxValue – minValue +1)) + minValue;

在上述公式中,minValue 是范围内的最小值,而 maxValue 则是范围内的最大值。例如,要获得 1018 的随机数,可以使用以下代码给变量 number 赋值:

const int MIN_VALUE = 10;const int MAX_VALUE = 18;number = rand() % (MAX_VALUE – MIN_VALUE + 1) + MIN_VALUE;

在上述代码中,(MAX_VALUE – MIN_VALUE + 1)的值为 9,这是目标范围内整数的个数。余数运算符(%)返回的值是 08 的数字,再用它加上 MIN_VALUE(也就是 10),即可获得 1018 的随机数。

————————

C + + provides a set of functions to generate and use random numbers. A random number is a value obtained by randomly selecting from a set of possible values. All values in this group have the same chance of being selected. Random numbers are commonly used in many different types of programs. Here are some examples:

  • Computer games usually use random numbers to simulate some random processes, such as rolling dice or dealing cards.
  • The simulation program uses random numbers to determine a series of subsequent operations or human and animal behaviors. You can create formulas that use random numbers to determine when a particular event occurs in your program.
  • The data analyzer may use random numbers to randomly select the data to be tested.
  • Computer security systems use random numbers to encrypt sensitive data.

The C + + library has a function called rand (), which returns a non negative integer every time it is called. To use the rand() function, you must include & lt; in your program; cstdlib> Header file. The following is an example of its usage:

randomNum = rand();

However, the number returned by this function is actually a pseudo-random number. This means that they have the performance and properties of random numbers, but they are not random in fact. They are actually generated by algorithms. The algorithm requires a starting value, called seed, to generate numbers. If a seed is not given, it will produce the same digital stream each time it runs. The following procedure illustrates this:

  • //This program demonstrates what happens in C++ if you
  • // try to generate random numbers without setting a “seed”.
  • #include
  • #include // Header file needed to use rand
  • using namespace std;
  • int main()
  • {
  • // Generate and printthree random numbers
  • cout << rand() << " ";
  • cout << rand() << " ";
  • cout << rand() << endl ;
  • return 0;
  • }

Output result of the first operation: 41 18467 6334 output result of the second operation: 41 18467 6334

To get a different stream of random numbers each time you run the program, you must provide a seed for the random number generator to start. In C + +, this is done by calling the srand function. Before Rand is called, the srand function must be called first, and srand is called only once in the whole program.

  • // This program demonstrates using random numbers when a
  • // “seed” is provided for the random number generator.
  • #include
  • #include // Header file needed to use srand and rand
  • using namespace std;
  • int main()
  • {
  • unsigned seed; // Random generator seed
  • // Get a nseed” value from the user
  • cout << "Enter a seed value: ";
  • cin >> seed;
  • // Set the random generator seed before calling rand()
  • srand(seed);
  • //Now generate and print three random numbers
  • cout << rand() << " ";
  • cout << rand() << " ";
  • cout << rand() << endl;
  • return 0;
  • }

First run result: enter a seed value: 19100 15331 – 209 second run result: enter a seed value: 171597 10689 28587

In the program, the variable seed created in line 9 to hold the seed is declared as unsigned. This data type holds only nonnegative integers. This is the data type that the srand function expects to receive during the call, so using the unsigned variable type can ensure that negative numbers will not be sent to srand. It can be seen from the output of the program that each time the program runs with different seeds, different random number streams will be generated. However, if you run the program again using 19 or 171 as a seed, you will get exactly the same number as the first time. In line 12 of the program, use CIN to obtain the value of the random number generator seed from the user’s input. In fact, another common way to get seed values is to call the time function, which is part of the C + + standard library. The time function returns the number of seconds elapsed since midnight on January 1, 1970, so it will provide a different seed value each time the program runs. The following program demonstrates the use of the time function. Note that you must pass it a parameter 0 when calling it. At the same time, the program contains a new header file CTime, which is necessary to use the time function.

  • //This program demonstrates using the C++ time function
  • //to provide a nseed,T for the random number generator.
  • #include
  • #include // Header file needed to use srand and rand
  • #include // Header file needed to use time
  • using namespace std;
  • int main()
  • {
  • unsigned seed; // Random generator seed
  • // Use the time function to get a “seed” value for srand
  • seed = time(0);
  • srand(seed);
  • // Now generate and print three random numbers
  • cout << rand() << " " ;
  • cout << rand() << " " ;
  • cout << rand() << endl;
  • return 0;
  • }

Program output results:

2961 21716 181

Limit the range of random numbers

Sometimes programs need a specific range of random numbers. To limit the range of random numbers to integers between 1 and a maximum value max, you can use the following formula:

number = rand() % max + 1;

For example, to generate a random number of 16 to represent the number of dice, you can use the following statement:

dice = rand() % 6 + 1;

Here is a brief introduction to its working principle. The remainder operator (%) can obtain the remainder after integer division. When the positive integer returned by the rand function is divided by 6, the remainder will be a number of 05. Because the target is a number of 16, you only need to add 1 to the remainder. This idea can be extended to random numbers in any range, and its general formula is as follows:

number = (rand()%(maxValue – minValue +1)) + minValue;

In the above formula, minValue is the minimum value in the range and maxvalue is the maximum value in the range. For example, to obtain a random number of 1018, you can assign a value to the variable number using the following code:

const int MIN_VALUE = 10;const int MAX_VALUE = 18;number = rand() % (MAX_VALUE – MIN_VALUE + 1) + MIN_VALUE;

In the above code, (max_value – min_value + 1) is 9, which is the number of integers in the target range. The value returned by the remainder operator (%) is the number of 08, and then add min to it_ Value (i.e. 10), you can obtain a random number of 1018.