简体   繁体   中英

generate uncorrelated number in the C language using rand()

I want to generate uncorrelated random number to do a simulation... However, the numbers generated by the rand() function in the C language are correlated.
Is there any possibility to use the rand() function and generate multiple random streams?
I mean, if the rand() function generate for me a series of correlated numbers, can I cut this series into different streams. Then use these streams independently?

Thanks

You are indeed correct. They are normally autocorrelated as the normal generator implementation is linear congruential (although the C standard does not mandate this). As such an xy plot of successive numbers will fail a chi square test for random 2D dispersion.

Depending on your application, you could look at Bays-Durham shuffle which, to my knowledge, passes the diehard test for randomness: it's aim is to defeat autocorrelation effects.

I direct you to www.nr.com for an implementation and the rand1 , rand2 functions in particular. A more modern way is to use a mersenne twister scheme but a little tricker to implement (by the way C++11 has this generator as part of its standard library).

If your C implementation has rand_r , you can try that. It lets you specify a location to store the state.

Or just use your own pseudo-random number generator.

您可以使用arc4random或更好的ar4random_uniform来增加生成值的随机性(实际上ar4random_uniform证明了统一分布的值)。

Generating true random numbers on a computer is impossible, you can only generate "pseudo-random" numbers ie numbers that "looks like" random.

Usually one will use a ''seed'' (small sequence of bits) with enough entropy and then "expand" it thanks to a Pseudo-Random-Number-Generator .

C rand() function generates poor quality of randomness, try PRNG that have been proposed in other answers/comments. Some examples :

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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