简体   繁体   English

在C中播种rand()与时间无关

[英]Seeding rand() in C independently of time

I am using stdlib to generate random numbers. 我正在使用stdlib生成随机数。 I know there are better generators but stdlib is quite enough for me. 我知道有更好的发电机,但stdlib对我来说足够了。

I am doing: 我在做:

while(condition){
    sleep(1);
    srand(time(NULL));
    r=rand();
}

It is inside a loop so I need sleep(1) or the seed is the same and the number is repeated. 它在一个循环内,所以我需要sleep(1)或种子是相同的,并重复数字。 The fact is that I need to generate thousands or maybe millions of numbers and if I wait 1 second per number it will take a long time. 事实是,我需要生成数千甚至数百万的数字,如果我每个数字等待1秒,则需要很长时间。 So there is another way independently of time to seed? 那么还有另一种独立于种子的方式吗?

You only need to seed once (at startup), then generate as many numbers as you like. 您只需要播种一次(启动时),然后生成任意数量的数字。 Don't reseed once per number - it's pointless, and you'd basically have to generate random seeds to generate random numbers (which rather defeats the purpose of using a PRNG in the first place). 不要为每个数字重新设置一次 - 这是毫无意义的,并且你基本上必须生成随机种子以生成随机数(这首先违背了使用PRNG的目的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM