如何调用srand多次影响随机性的质量? [英] How does calling srand more than once affect the quality of randomness?

查看:470
本文介绍了如何调用srand多次影响随机性的质量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

评论说明: / p>


srand(time(0)); main()
,而不是多次调用它(实际上会导致
随机数
)。


...我已经粗化了我遇到的问题...重复常见的建议,调用 srand 。像 srand()这样的问题 - 为什么只调用一次?重复一遍,因为 time(0)返回当前时间(以秒为单位),在同一秒内多次调用 srand 将产生相同的种子。一个常见的解决方法是改为使用毫秒或纳秒。



但是,我不明白为什么这意味着 srand 应该或只能调用一次,或如何导致较少随机数字。



cppreference


说,伪随机数生成器应该只有
种子一次,在任何调用rand()之前,并且程序的开始。
它不应该重复播种,或者每次你希望生成一批新的随机数字时重新播种。


phoxis srand() - 为什么只调用一次?


初始化一次带有种子值的初始状态将产生
个足够的随机数,因为你不用srand设置内部状态
,因此使数字更可能是随机的。


也许他们只是使用不精确的语言,没有任何解释似乎解释为什么调用 srand 多次是坏的(除了产生相同的随机数序列)或它如何影响数字的随机性。

解决方案

查看 srand()的来源来自此问题: Rand实施



  static unsigned long int next = 1; 

int rand(void)// RAND_MAX假定为32767
{
next = next * 1103515245 + 12345;
return(unsigned int)(next / 65536)%32768;
}

void srand(unsigned int seed)
{
next = seed;
}

正如你所看到的,当你调用 srand时间(0))你会得到 rand()取决于种子的新数字。数字将在几个月后重复,但再次调用 srand 会使其他。无论如何,它必须在一些周期后重复 - 但顺序取决于 srand 的参数。这就是为什么C rand不适合加密 - 你可以预测下一个数字,当你知道种子。



如果你有快速循环,调用 srand 每次迭代都没有意义 - 你可以得到相同的数字, code> time()(1秒是现代CPU的非常大的时间)给另一个种子。



简单的应用程序调用srand多次 - 这个发电机是弱设计,如果你想要真正的随机数,你必须使用其他(最好的我知道是 Blum Blum Shub



对我来说,没有更多或更少的随机数 - 它总是取决于种子,如果你使用相同的种子,它们重复。使用时间是很好的解决方案,因为它很容易实现,但你必须只使用一个( main()的开头)或当你确定调用 srand(time(0))在另一秒钟。


This comment, which states:

srand(time(0)); I would put this line as the first line in main() instead if calling it multiple times (which will actually lead to less random numbers).

...and I've bolded the line which I'm having an issue with... repeats common advice to call srand once in a program. Questions like srand() — why call only once? re-iterate that because time(0) returns the current time in seconds, that multiple calls to srand within the same second will produce the same seed. A common workaround is to use milliseconds or nanoseconds instead.

However, I don't understand why this means that srand should or can only be called once, or how it leads to less random numbers.

cppreference:

Generally speaking, the pseudo-random number generator should only be seeded once, before any calls to rand(), and the start of the program. It should not be repeatedly seeded, or reseeded every time you wish to generate a new batch of pseudo-random numbers.

phoxis's answer to srand() — why call only once?:

Initializing once the initial state with the seed value will generate enough random numbers as you do not set the internal state with srand, thus making the numbers more probable to be random.

Perhaps they're simply using imprecise language, none of the explanations seem to explain why calling srand multiple times is bad (aside from producing the same sequence of random numbers) or how it affects the "randomness" of the numbers. Can somebody clear this up for me?

解决方案

Look at the source of srand() from this question: Rand Implementation

Also, example implementation from this thread:

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;
}

As you can see, when you calling srand(time(0)) you will got new numbers on rand() depends on seed. Numbers will repeat after some milions, but calling srand again will make it other. Anyway, it must repeat after some cycles - but order depends on argument for srand. This is why C rand isn't good for cryptography - you can predict next number when you know seed.

If you have fast loop, calling srand every iteration is without sense - you can got same number while your time() (1 second is very big time for modern CPUs) give another seed.

There is no reason in simple app to call srand multiple times - this generator are weak by design and if you want real random numbers, you must use other (the best I know is Blum Blum Shub)

For me, there is no more or less random numbers - it always depends on seed, and they repeat if you use same seed. Using time is good solution because it's easy to implement, but you must use only one (at beginning of main()) or when you sure that you calling srand(time(0)) in another second.

这篇关于如何调用srand多次影响随机性的质量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆