Re: Random generator

From: Sandman (mr_at_sandman.net)
Date: 04/13/04


Date: Tue, 13 Apr 2004 09:15:02 +0200

In article <6HFec.2251$i67.22112301@news-text.cableinet.net>, "Garp"
<garp7@no7.blueyonder.co.uk> wrote:

>>> I am using the same algorithm, but using Random and as you say, they
>>> generate the random numbers differently, I've not time to now to try
>>> out your answer above, but what does it actually mean?
>>>
>>> They way I understood the VB and PHP randomiser for that matter, is
>>> that if I seed them, I will get a random number each time but its a
>>> psudo random number, in so much as each time I call it the random
>>> numbers are repeatable.
>>
>> How do you know you're using the same pseudo-random algorithm? The
>> rand function in PHP uses the C rand function, which may differ from
>> C library to C library.
>
> Almost certainly.
>
> What the PRNG system does is calculate a seemingly random number based
> on a seed value, using an algorithm like the one I posted above. That
> will produce a predictable sequence from known seeds, but seed it from
> time() and it will be as random as you need.

I suggest using microtime() instead, if you may want to access a random value
more than once in a second.

function getmicrotime(){
    list($usec, $sec) = explode(" ",microtime());
    return ((float)$usec + (float)$sec);
}

-- 
Sandman[.net]


Relevant Pages

  • Re: Random generator
    ... >> function in PHP uses the C rand function, which may differ from C library ... using an algorithm like the one I posted above. ... >produce a predictable sequence from known seeds, but seed it from timeand ... from either Java or PHP. ...
    (comp.lang.php)
  • Re: [PHP] Rand() problem
    ... I am running the latest stable PHP release. ... The rand function worked a few times then died on ... > n> was using Xitami) and it gave me the same results. ...
    (php.general)
  • Re: Lots of 2s
    ... It's just a few lines of php that finds images in a specified folder and displays them seemingly at random. ... I can't see where it gets the randomness from as it doesn't use the rand function. ...
    (uk.misc)
  • Re: Random generator
    ... > They way I understood the VB and PHP randomiser for that matter, ... How do you know you're using the same pseudo-random algorithm? ... function in PHP uses the C rand function, which may differ from C library to ...
    (comp.lang.php)