As php.net indicates: random_int()
function Generates cryptographically secure pseudo-random integers.
But, Can someone explain whats the difference between rand()
& random_int()
? Can I use random_int()
instead of rand()
when only requiring a random integer? Which one is faster?
Unless speed is an issue, random_int() is the way to go but for the most basic non critical missions. Show activity on this post. As most number generators, using rand() is not secure because it does not generate cryptographically secure values and the output of rand() is predictable.
The mt_rand() function is a drop-in replacement for the older rand(). It uses a random number generator with known characteristics using the » Mersenne Twister, which will produce random numbers four times faster than what the average libc rand() provides.
Definition and Usage The rand() function generates a random integer. Example tip: If you want a random integer between 10 and 100 (inclusive), use rand (10,100). Tip: As of PHP 7.1, the rand() function has been an alias of the mt_rand() function.
From http://php.net/manual/en/function.mt-rand.php: Caution This function does not generate cryptographically secure values, and should not be used for cryptographic purposes.
Revisiting the question and seeing there's been an answer given, I find it's only fair that I submit my comments to an answer, seeing they were submitted before.
The manual on PHP 7's random_int()
function states:
"Returns a cryptographically secure random integer in the range min to max, inclusive."
and for rand()
*This function does not generate cryptographically secure values" *
OP's comment:
"@Fred-ii- thank you. But what does "cryptographically secure pseudo-random" mean? – NDFA"
That can be found in the following links as per my findings:
Which states:
A cryptographically secure pseudo-random number generator (CSPRNG) or cryptographic pseudo-random number generator (CPRNG)[1] is a pseudo-random number generator (PRNG) with properties that make it suitable for use in cryptography.
In regards to performance, you will need to run a benchmark yourself.
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