Why are initial random numbers similar when using similar seeds?

后端 未结 4 1349
说谎
说谎 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:49

    This is a fairly typical behaviour for pseudo-random seeds - they aren't required to provide completely different random sequences, they only provide a guarantee that you can get the same sequence again if you use the same seed.

    The behaviour happens because of the mathematical form of the PRNG - the Java one uses a linear congruential generator, so you are just seeing the results running the seed through one round of the linear congruential generator. This isn't enough to completely mix up all the bit patterns, hence you see similar results for similar seeds.

    Your best strategy is probably just to use very different seeds - one option would be to obtain these by hashing the seed values that you are currently using.

提交回复
热议问题