I know that it is encouraged to use mt_rand()
over rand()
because it uses the Mersenne Twister over whatever PRNG rand()
uses, but here's something that never seems to be factored in: user activity.
In fact, the actions of users can be considered pretty random. For instance, at any given moment, there might be a 4% chance a user might trigger a rand()
call for one feature, an 8% chance of a user triggering three rand()
calls and a shuffle()
, a 20% of a user triggering two rand()
calls, and every time a user loads a page the PRNG advances by one.
After all, isn't NPC movement what makes RNG-abuse in Pokémon games so frustrating?
So, bearing in mind that, while rand()
does have its patterns, is the randomness of the users' activities and the variety of uses of rand()
enough to make rand()
's shortcomings irrelevant? In absolute terms, mt_rand()
is "more random". But how does this compare to the entropy of the human element?
PHP 7.1: The rand () function is an alias of mt_rand (). PHP 4.2.0: The random number generator is seeded automatically.
This means that sequences generated with a specific seed may differ from PHP 7.1 on 64-bit machines. rand () has been made an alias of mt_rand () . min max range must be within the range getrandmax (). i.e. ( max - min) <= getrandmax () Otherwise, rand () may return poor-quality random numbers. I also enjoy making one-liners.
This function is available in PHP versions 4.x, PHP 5.x as well as PHP 7.x. This example returns random number by calling rand () without arguments − This may produce following result (it being a random number, it is more likely to return different number every time)−
The rand () function returns an integer using pseudo random genaration technique. default range is between 0 and platform specific getrandmax (). On 64 bit Windows OS, it is 2147483647. The rand () function can be called without arguments (in which case the default range will be used) or by specifying min and max parameters.
If you assume calls to rand() are generated by human users at random times then i guess your logic is correct.
However imagine a bot that sends same requests each X seconds (at night hours not interrupted by human calls) or a simple script that runs for a given amount of time and runs rand() one by one. Then you can not fully depend on randomness.
comment from php.net :
Note that the automatic seeding seems to be done with the current number of seconds which means you can get the same results for several runs on a fast server. Either call srand() yourself with a more frequently changing seed or use mt_rand() which doesn't appear to suffer from the problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With