Re: Reseeding rand()/mt_rand()

From: Date: Tue, 31 Jan 2017 13:07:35 +0000
Subject: Re: Reseeding rand()/mt_rand()
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-98080@lists.php.net to get a copy of this message
On 30.01.2017 at 17:29, Andrea Faulds wrote: > Christoph M. Becker wrote: > >> Just a quick idea: >> >> <?php >> >> class PNRG { >> public function __construct($seed = null) {…} >> public function get() {…} >> } > > I've long favoured an API along these lines. One size does not fit all, > you need two APIs for (non-crytographic) random numbers: > - automatically-seeded, non-reproducible numbers (a global function like > rand()) > - manually-seeded, reproducible number sequences (objects) > > Currently we mix these two, and it's a mess. The easy solution would be > a random number-generating object like you propose, to cover the second > use-case. We could also introduce a non-manually-seedable global PRNG > also, if desired. Perhaps as a static method of that class. > > One thought: it'd be nice if you had two different methods, one that > mutates in places, and one that doesn't, i.e.: > > $prng1 = new PRNG(SEED); > [$randomNumber, $prng2] = $prng2->getWithNewPRNG(); > > v.s.: > > $prng = new PRNG(SEED); > $randomNumber = $prng->get(); > $randomNumber2 = $prng->get(); Thanks, Andrea! I like all your suggestions, and I'd be happy to see a respective RFC – unfortunately, I won't have time to do this myself in the near future. -- Christoph M. Becker

« previous php.internals (#98080) next »