Why are initial random numbers similar when using similar seeds?

后端 未结 4 1354
说谎
说谎 2020-12-03 09:57

I discovered something strange with the generation of random numbers using Java\'s Random class. Basically, if you create multiple Random objects using close seeds (for exam

4条回答
  •  没有蜡笔的小新
    2020-12-03 10:48

    I wouldn't have called this an "issue".

    And also, do you know if this problem only for the first value (or first few values), or if it is more general and using close seeds should be avoided?

    Correlation patterns between successive numbers is a common problem with non-crypto PRNGs, and this is just one manifestation. The correlation (strictly auto-correlation) is inherent in the mathematics underlying the algorithm(s). If you want to understand that, you should probably start by reading the relevant part of Knuth's Art of Computer Programming Chapter 3.

    If you need non-predictability you should use a (true) random seed for Random ... or let the system pick a "pretty random" one for you; e.g. using the no-args constructor. Or better still, use a real random number source or a crypto-quality PRNG instead of Random.


    For the record:

    1. The javadoc (Java 7) does not specify how Random() seeds itself.
    2. The implementation of Random() on Java 7 for Linux, is seeded from the nanosecond clock, XORed with a 'uniquifier' sequence. The 'uniquifier' sequence is LCG which uses different multiplier, and whose state is static. This is intended to avoid auto-correlation of the seeds ...

提交回复
热议问题