PrimitiveType

PHP: generate a random number

<?php // Generate a random integer between 0 and getrandmax() $randomNum = rand(); // Generate a random integer between min & max (inclusive) - here min & max are 1 & 10, respectively $randomNum = rand(1, 10); // min and max can be reversed $randomNum = rand(10, 1); // Use the faster mt_rand() function to generate a random number between 0 and mt_getrandmax() $randomNum = mt_rand(); // Generate a random integer between min & max (inclusive) $randomNum = mt_rand(1, 10);

Note that mt_rand() will return false if max is less than min.

The above functions are not cryptographically secure. If you are using PHP 7 use random_int() to generate cryptographically secure values.