Fast real valued random generator in java

后端 未结 6 1528
借酒劲吻你
借酒劲吻你 2020-12-31 12:51

java.util.Random.nextDouble() is slow for me and I need something really fast.

I did some google search and I\'ve found only integers based fast random generators.

6条回答
  •  余生分开走
    2020-12-31 13:26

    If you need something fast and have access to Java8, I can recommend the java.utils SplitableRandom. It is faster (~twice as fast) and has better statistical distribution.

    If you need a even faster or better algorithm I can recommend one of these specialized XorShift variants:

    • XorShift128PlusRandom (faster & better)
    • XorShift1024StarPhiRandom (similar speed, even longer period)

    Information on these algorithms and their quality can be found in this big PRNG comparison.

    I made an independent Performance comparison you can find the detailed results and the code here: github.com/tobijdc/PRNG-Performance

    TLDR

    Never use java.util.Random, use java.util.SplittableRandom. If you need faster or better PRNG use a XorShift variant.

提交回复
热议问题