C++ 中的 rand() 和 srand()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15188049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:08:46  来源:igfitidea点击:

rand() and srand() in C++

c++random

提问by Manoj Pandey

What is the basis of generating random numbers in C++?

C++中生成随机数的基础是什么?

Is there some logic or principle behind that?

Are the numbers generated completely random?

这背后有什么逻辑或原则吗?

生成的数字是完全随机的吗?

Suppose I am running this program:

假设我正在运行这个程序:

#include <iostream.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    /*
    Declare variable to hold seconds on clock.
    */
    time_t seconds;
    /*
    Get value from system clock and
    place in seconds variable.
    */
    time(&seconds);
    /*
    Convert seconds to a unsigned
    integer.
    */
    srand((unsigned int) seconds);
    /*
    Output random values.
    */
    cout<< rand() << endl;
    cout<< rand() << endl;
    cout<< rand() << endl;
    return 0;
}

What it shows: http://img14.imageshack.us/img14/1538/98271820.png

它显示的内容:http: //img14.imageshack.us/img14/1538/98271820.png

It showed 205 twice.

它显示了 205 两次。

采纳答案by Mats Petersson

Starting with the second question:

从第二个问题开始:

Are the numbers generated completely random?

生成的数字是完全随机的吗?

No, that is very unlikely to ever happen in a computer. They are "pseudo-random" numbers, which is some sequence of numbers that vary in range over time in a random-like fashion. But if you start with the same "seed", you get the same sequence each time. This predictability is sometimes very useful, as it allows repeating the same experiment several times with the same outcome - altering the seed, will allow a similar run to have a different outcome.

不,这在计算机中不太可能发生。它们是“伪随机”数字,它是一些以类似随机方式随时间变化范围内的数字序列。但是如果你从同一个“种子”开始,你每次都会得到相同的序列。这种可预测性有时非常有用,因为它允许以相同的结果多次重复相同的实验——改变种子,将允许类似的运行产生不同的结果。

The function srandsets the seed. Some systems do have a function called randomize, but it is not part of the standard as such. If it does exist it sets the seed to something unknown to the code - such as the current time in milliseconds.

该函数srand设置种子。有些系统确实有一个名为 的函数randomize,但它本身并不是标准的一部分。如果它确实存在,它将种子设置为代码未知的东西 - 例如以毫秒为单位的当前时间。

Is there some logic or principle behind that?

这背后有什么逻辑或原则吗?

Yes. There are several methods for generating pseudo-randum numbers. Simple ones can be written in one or two lines of C code using regular intor longtypes, and just consists of taking the "current value" + some constant, multiplied by some large number and modulo some other large number.

是的。有几种生成伪随机数的方法。简单的可以使用正则intlong类型用一两行 C 代码编写,只需取“当前值”+某个常数,乘以某个大数并取模其他大数即可。

More complex ones involve dozens of more lines of rather complicated math with large numbers - for example Mersenne Twister is a recent work that is available as source code if you search a little bit.

更复杂的涉及大量复杂数学的数十行 - 例如 Mersenne Twister 是最近的作品,如果您稍微搜索一下,就可以作为源代码获得。

回答by DarkWanderer

The question was basically answered in comments and another answer, but I'll gather it up in one place.

这个问题基本上在评论和另一个答案中得到了回答,但我会把它收集到一个地方。

C++ rand()function produces not a truly random sequence of numbers, but a pseudo-random one. This means that it is basically a pre-defined sequence of numbers which are "random", but fixed somewhere (actually, it's more complex than that, but this is a simplification for better understanding). Think of it as a long list of integers.

C++rand()函数产生的不是真正随机的数字序列,而是伪随机序列。这意味着它基本上是一个预定义的数字序列,它们是“随机的”,但固定在某处(实际上,它比那更复杂,但这是为了更好地理解而进行的简化)。将其视为一长串整数。

Each call to rand()function pulls the current number and moves the pointer to "current "random" number" to the next one.

每次调用rand()函数都会拉出当前数字并将指向“当前“随机”数字的指针移动到下一个。

What srand()function does is basically setting the pointer to some location in the list. If you don't call the srand()function on each launch, or call it with fixed parameter (seed), you will have the same sequence of numbers on each program launch.

什么srand()功能呢,基本上设定指针列表中的某个位置。如果您不在srand()每次启动时调用该函数,或使用固定参数(种子)调用它,则每次程序启动时您将拥有相同的数字序列。

When you're setting your seed from the seconds, if you launch your program twice within that second, your seed will be the same - hence producing the same result.

当你从几秒开始设置你的种子时,如果你在那一秒内启动你的程序两次,你的种子将是相同的 - 因此产生相同的结果。

Try the following code:

试试下面的代码:

#include <windows.h>
// << other code >>
for (int i=0; i<50; i++) {
    time(&seconds);
    srand(seconds);
    cout<< seconds<<" "<<rand()<<endl;
    Sleep(100);
}

You will notice, that each "seconds" value correspond to some fixed "first" value for the rand()function.

您会注意到,每个“秒”值对应于rand()函数的某个固定“第一个”值。