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?
As of PHP 7.1,
rand()
is basically an alias formt_rand()
. The newerrandom_int()
is the slowest, but only secure method of the three.Results:
As most number generators, using rand() is not secure because it does not generate cryptographically secure values and the output of rand() is predictable.
PHP 7.0 introduces random_bytes and random_int as core functions which are free from the problems that most of random number generators have.
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:and for
rand()
OP's comment:
That can be found in the following links as per my findings:
Which states:
In regards to performance, you will need to run a benchmark yourself.
I have not personally encountered any problems using random_int but it should be used with try/catch as it throws an exception if it was not possible to gather sufficient entropy.