Is there a way to generate a random number based on a min and max?
For example, if min was 1 and max 20 it should generate any number between 1 and 20, including 1 and 20?
Is there a way to generate a random number based on a min and max?
For example, if min was 1 and max 20 it should generate any number between 1 and 20, including 1 and 20?
I have bundled the answers here and made it version independent;
A
quickerfaster version would use mt_rand:Source: http://www.php.net/manual/en/function.mt-rand.php.
NOTE: Your server needs to have the Math PHP module enabled for this to work. If it doesn't, bug your host to enable it, or you have to use the normal (and slower) rand().
or
http://php.net/manual/en/function.rand.php
In a new PHP7 there is a finally a support for a cryptographically secure pseudo-random integers.
which basically makes previous answers obsolete.
Docs for PHP's rand function are here:
http://php.net/manual/en/function.rand.php
Use the
srand()
function to set the random number generator's seed value.