I need a PHP function that generate random number identical to javascript Math.random() width the same seed.
MDN about math.random():
The random number generator is seeded from the current time, as in Java.
As far I tried the PHP's rand() generates something like that:
srand( time() ); // I use time as seed, like javascript does
echo rand();
Output: 827382
And javascript seems to generate random numbers on it's own way:
Math.random(); Output: 0.802392144203139
I need PHP code that is equivalent for math.random(), not new javascript code. I can't change javascript.
You could use a function that returns the value:
PHP
JavaScript
@elclanrs solution is easier and doesn't need the cast in return.
Update
There's a good question about the difference between PHP
mt_rand()
andrand()
here:What's the disadvantage of mt_rand?
You could try:
Or even:
However, @elclanrs solution seems a lot easier. I tried.
Javascript's Math.random gives a random number between 0 and 1. Zero is a correct output, but 1 should not be included. @thiagobraga's answer could give 1 as an output. My solution is this:
This will give a random number between 0 and 0.99999999953434.