Hi Andrea,

On Tue, Jan 31, 2017 at 1:29 AM, Andrea Faulds <a...@ajf.me> 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)
>

I agree. We should have PRNG object.


>
> 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.
>

Mixing system and user seeded PRNG is mess. I totally agree.
Let's divide them into separated states.


>
> 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();


My current objective is to make existing API to work, so resource may be
used
to set/get PRNG state.

*** Initialize and Create new PRNG state resource ***
resource mt_srand([int|string $seed])

resource: MT rand state resource.
$seed: I would like to allow large seed like Python and Ruby.
            When seed is string, use all bits for MT rand state
initialization.
            Example usage: mt_srand(random_bytes(2000));

*** Get random number from state resource ***
int mt_rand() // System state
int mt_rand(resource $state) // Specified user state
int mt_rand(int $min , int $max [,resource $state]) // Specified user state
if $state is passed



I suppose most codes do not use mt_srand()/srand(). With new API, users may
write
code like

===============
// We need the same random numbers here
$my_state = mt_srand(1234);
// NOTE: mt_srand(), w/o seed, creates new PRNG state.
for ($i=0; $i < 10; $i++) {
   // Use my PRNG state
   $my_rand[] = mt_rand($my_state);
}

Somewhere later in code

// We need somewhat random numbers for non CS purpose
for ($i=0; $i < 10; $i++) {
   // System PRNG state is affected by mt_srand()/srand()
   // and $my_other_rand will have proper random values
   $my_other_rand[] = mt_rand();
}
===============

This requires code modifications, but it isn't too bad.
Problem is this change would be master only and
other functions, e.g. shuffle(), have optional state resource
parameter or not.

Thoughts? Comments?

Regards,

--
Yasuo Ohgaki
yohg...@ohgaki.net

Reply via email to