Re: Reseeding rand()/mt_rand()
| From: | Andrea Faulds | Date: | Mon, 30 Jan 2017 16:29:43 +0000 |
| Subject: | Re: Reseeding rand()/mt_rand() | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-98044@lists.php.net to get a copy of this message | ||
Hi Christoph,
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 Faulds
https://ajf.me/