reproducible random number series

后端 未结 3 2072
心在旅途
心在旅途 2021-02-20 10:10

How can i get a series of reproducible pseudorandom numbers in PHP?

In older versions of PHP i could do that just by using the same seed in the RNG, but it does not work

相关标签:
3条回答
  • 2021-02-20 10:38

    This is not best one but it is a work one

    function ranseed($min, $max, $seed) {
        return round($min + (hexdec(md5($seed)) / hexdec("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")) * ($max - $min));
    }
    
    0 讨论(0)
  • 2021-02-20 10:54

    The Mersenne Twist is a nice fast PRNG and here's a public domain PHP implementation for it:

    http://kingfisher.nfshost.com/sw/twister/

    That only works on PHP 5.3.0 and above.

    0 讨论(0)
  • 2021-02-20 10:59

    One of the best random number algorithms by some metrics is Mersenne Twister. You can find a pure PHP version here (there are others).

    You can then call:

    init_with_integer($integer_seed)
    

    and get the same output (for a given seed) every time.

    0 讨论(0)
提交回复
热议问题