如何兰特()做出的随机数,即使你不随时间(NULL)种子呢? [英] How can rand() make random numbers, even when you don't seed it with time(NULL)?
问题描述
我明白什么是传递到函数srand()
作为参数将作为种子调用兰特()
这样以后发生。一般时间(NULL)
在过去。
但是如果你只叫函数srand
一次,然后你兰特
正在不断地在循环中调用,怎么是数字各不相同?种子是从时间(NULL)返回的值
...但只使用一个特定的返回值。因此,如何正在生成不同的号码?
我不每次使用线程和不同的种子了解线程在C.什么,但为函数srand
,以便后续调用到兰特
其实并返回随机数?我注意到,当我通过一个静态的数函数srand
而不是时间(NULL)
的兰特
仍将产生随机数。
草案C99标准提供了部分样本实施 7.20.2.2
的 srand函数的,这将很可能清理它是如何为你工作:
实例下面的函数定义一个移植实现兰特和函数srand的。
静态unsigned long int类型旁边= 1;
INT兰特(无效)// RAND_MAX假设为32767
{
接下来=下一个* 1103515245 + 12345;
收益率(无符号整数)(下一个/ 65536)%32768;
}无效函数srand(无符号整数种子)
{
接下来=种子;
}
块引用>
函数srand
设置的静态的变量接下来
,然后设置为新值兰特
,然后后续调用兰特
将搭载与的previous价值接下来
。例如,如果我们使用
函数srand(1)
然后顺序应该是这样的,因为我们做出兰特通话
:下一= 1
接下来= 1 * 1103515245 + 12345 = 1103527590
接下来= 1103527590 * 1103515245 + 12345 = 1217759518843121895
接下来= 1217759518843121895 * 1103515245 + 12345 = 15426401326682203284由于HostileFork指出,数字不是真正的随机,但伪,它说:
也被称为确定性随机位产生器(DRBG),[1]是一种
算法用于产生一个数字序列,其属性
近似随机数序列的性质。该
PRNG生成序列不是真正随机的,因为它是完全
由相对小的组初始值决定的,称为
PRNG的种子(其可以包括真正的随机值)。虽然
真正随机的可使用产生更接近序列
硬件随机数发生器,伪随机数发生器
在实践中重要的是其在数产生和它们的速度
重复性。
块引用>此特定的实现是使用线性同余方法。
I understand that what's passed into
srand()
as an argument will be used as the seed for calls torand()
that happen afterward. Generallytime(NULL)
is passed in.But if you only call
srand
once, and then you haverand
being called continuously in a loop, how are the numbers different from each other? The seed was the value returned fromtime(NULL)
...but ONLY that one specific return value is being used. So how are different numbers being generated?I don't know anything about threading in C. But is
srand
using threads and a different seed each time so that subsequent calls torand
actually do return random numbers? I noticed that when I pass a static number tosrand
instead oftime(NULL)
, therand
is still generating random numbers.解决方案The draft C99 standard provides a sample implementation in section
7.20.2.2
The srand function that will probably clear up how it work for you:EXAMPLE The following functions define a portable implementation of rand and srand.
static unsigned long int next = 1; int rand(void) // RAND_MAX assumed to be 32767 { next = next * 1103515245 + 12345; return (unsigned int)(next/65536) % 32768; } void srand(unsigned int seed) { next = seed; }
srand
set the static variablenext
which is then set to new value inrand
and then subsequent calls torand
will pick up with the previous value ofnext
.For example if we used
srand(1)
then the sequence would look like this as we make calls torand
:next = 1 next = 1 * 1103515245 + 12345 = 1103527590 next = 1103527590 * 1103515245 + 12345 = 1217759518843121895 next = 1217759518843121895 * 1103515245 + 12345 = 15426401326682203284
As HostileFork points out, the numbers are not truly random but are Pseudorandom, which says:
also known as a deterministic random bit generator (DRBG),[1] is an algorithm for generating a sequence of numbers whose properties approximate the properties of sequences of random numbers. The PRNG-generated sequence is not truly random, because it is completely determined by a relatively small set of initial values, called the PRNG's seed (which may include truly random values). Although sequences that are closer to truly random can be generated using hardware random number generators, pseudorandom number generators are important in practice for their speed in number generation and their reproducibility.
This particular implementation is using a Linear congruential generator.
这篇关于如何兰特()做出的随机数,即使你不随时间(NULL)种子呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!