C++ srand(time(0)) 和随机数生成
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4736485/
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
srand(time(0)) and random number generation
提问by Simplicity
srand(time(0))
is used in C++ to help in the generation of random numbers by seeding rand with a starting value.
在 C++ 中使用 rand 来帮助生成随机数,方法是用初始值播种 rand。
But, can you explain what it exactly does?
但是,你能解释一下它到底是做什么的吗?
Thanks.
谢谢。
回答by orlp
srand()
gives the random function a new seed, a starting point (usually random numbers are calculated by taking the previous number (or the seed) and then do many operations on that number to generate the next).
srand()
给随机函数一个新的种子,一个起点(通常随机数是通过取前一个数字(或种子)来计算的,然后对该数字进行多次操作以生成下一个)。
time(0)
gives the time in seconds since the Unix epoch, which is a pretty good "unpredictable" seed (you're guaranteed your seed will be the same only once, unless you start your program multiple times within the same second).
time(0)
给出自 Unix 纪元以来的时间(以秒为单位),这是一个非常好的“不可预测”种子(你保证你的种子只会相同一次,除非你在同一秒内多次启动你的程序)。
回答by user489041
From what I remember, there is an equation that is used to generate a sequence of values. The next value is somewhat influenced by the previous value. By using the time, you are setting the initial value of the equation. Keep in mind, the values are pseudo random.
据我所知,有一个方程用于生成一系列值。下一个值在某种程度上受前一个值的影响。通过使用时间,您正在设置方程的初始值。请记住,这些值是伪随机的。
So for example, if you do something like this:
例如,如果您执行以下操作:
srand(1);
srand(1);
The same sequence of numbers will be generated. However, if you do:
将生成相同的数字序列。但是,如果您这样做:
srand(time(0));
srand(time(0) + 1);
Two different sequences of numbers will b e generated because the seed value is different.
由于种子值不同,将生成两个不同的数字序列。
回答by peoro
The C rand
function generates random numbers using a seed (just like most -- any? -- pseudo-random generator).
Crand
函数使用种子生成随机数(就像大多数 - 任何? - 伪随机生成器)。
srand
is used to set the seed to be used by the random generator algorithm. The value returned by time
(ie: the current second) is usually passed to such function because it's the most simple-to-get value that is usually different between two executions of an application
srand
用于设置随机生成器算法要使用的种子。由time
(即:当前秒)返回的值通常被传递给这样的函数,因为它是最容易获取的值,通常在应用程序的两次执行之间是不同的
回答by Necrolis
firstly, srand
& rand
is for/from C, not C++, C++0x is introducing its own random number generator classes.
首先,srand
&rand
是用于/来自 C,而不是 C++,C++0x 正在引入它自己的随机数生成器类。
Other than that srand
& rand
are implementation defined, they only need deliver a random between 0 and RAND_MAX
. On windowsits a basic MWC16/32, that uses TLS to store seeds, srand
sets that seed, which is then used by rand
to roll a pseudo random number.
除了srand
&rand
是实现定义的之外,它们只需要提供 0 和 之间的随机数RAND_MAX
。在Windows 上,它是一个基本的MWC16/32,它使用 TLS 来存储种子,srand
设置该种子,然后使用它rand
来滚动伪随机数。
By way of random number generators its pretty terrible, especially the windows version.
通过随机数生成器,它非常糟糕,尤其是 Windows 版本。
回答by eaj
There's an article at Wikipediathat gives some good history and examples of algorithms used.
The short version is that rand()
and its ilk are pseudorandom number generators. In fact, they're actually deterministic—the sequence of numbers produced is always the same. However, the sequence is very very long, and the seeding mechanism provides a means of starting at a (more or less) arbitrary place in that sequence.
简短的版本是,rand()
它的同类是伪随机数生成器。事实上,它们实际上是确定性的——产生的数字序列总是相同的。然而,该序列非常非常长,并且播种机制提供了一种从该序列中(或多或少)任意位置开始的方法。
Depending on how the random numbers will be used, there are a variety of criteria for evaluating the quality of a pseudorandom generator. In very simple circumstances, perhaps all that's needed is a low probability of repeated values from consecutive calls to rand()
. However, you're likely to want the numbers to conform to a particular statistical distribution as well. (As far as I know, most PRNGs produce uniformly distributed numbers. However various functions exist or can be easily written to transform this into a gaussian distribution or any other you might need.)
根据随机数的使用方式,有多种标准可用于评估伪随机生成器的质量。在非常简单的情况下,可能只需要从连续调用rand()
. 但是,您可能还希望这些数字符合特定的统计分布。(据我所知,大多数 PRNG 产生均匀分布的数字。但是存在各种函数或可以轻松编写将其转换为高斯分布或您可能需要的任何其他函数。)
Finally, when security is a concern, you want an algorithm that is, for all practical purposes, unpredictable. If an attacker knows the previously generated number, she shouldn't be able to then anticipate the next. In very high security applications, a truly random number generator might be used; these would rely on various external sources of randomness, such as radio static, the thermal noise in an image capture device (lens cap on), or other such sources. Also, many modern operating systems (including Linux) gather and store an "entropy pool" from various sources such as unpredictable user interactions and allow the production of random numbers from that.
最后,当安全是一个问题时,您需要一种算法,对于所有实际目的而言,都是不可预测的。如果攻击者知道先前生成的数字,则她应该无法预测下一个数字。在安全性非常高的应用中,可能会使用真正的随机数生成器;这些将依赖于各种外部随机源,例如无线电静电、图像捕获设备(镜头盖)中的热噪声或其他此类源。此外,许多现代操作系统(包括 Linux)从各种来源(例如不可预测的用户交互)收集和存储“熵池”,并允许从中产生随机数。
A common programming error from people not used to working with pseudorandom generators is to re-seed before every call to rand()
. Of course, this is unnecessary and probably undesirable. Seed each generator only once.
不习惯使用伪随机生成器的人的一个常见编程错误是在每次调用rand()
. 当然,这是不必要的并且可能是不可取的。每个生成器只播种一次。
回答by R.M.VIVEK Arni
#include "stdio.h" //rmv coding for srand is pure
#include "conio.h"
#include "time.h"
void main()
{
time_t t;
srand(time(0));
for (i = 1; i <= 10; i++)
cout << (unsigned) rand() % 1000 - 90 << "\t";
for (i = 1; i <= 10; i++)
cout << (char) rand() % 100 - 90 << "\t";
getch();
}