Random numbers can be generated in many ways, some of which are cryptographically secure others are not. Inbuilt functions like rand() and random_int() can be used to get random numbers with in a range.
Using rand() Function: The rand() function generates a pseudo random number between the given range or between 0 and the default max value (getgrandmax()) which depends on the system.
int rand( $min, $max )
Parameters: The rand() function accepts two optional parameters as mentioned above and described below.
- $min: It is an optional parameter and used to set the lower limit for the random number. The default value of min is 0.
- $max: It is an optional parameter and used to set the upper limit for the random number. The default value of max is the return value of getgrandmax(), which depends on the system (for Windows it is 32767).
Program 1: PHP program to generate random number using rand() function.
Random number: 77551982 Random number in range (7, 100): 37
Note: The rand() function is pseudo random function that means it takes the seed from the machine and generate the number according to it. So, the number generation method is not totally random. It can be tracked up to certain extent. So it is not cryptographically secure. It is not used for cryptography where randomization is very important. To generate cryptographically secure, random number use random_int() function.
Using random_int() Function: The random_int() function is used to generate cryptographically secure random numbers. These numbers can be used for unbiased results. The function CryptGenRandom() in Windows and getrandom(2) system call in Linux to generate the random number.
int random_int( $min, $max )
Parameters: The random_int() function accepts two parameters as mentioned above and described below.
- $min: It holds the lower limit of the random number.
- $max: It holds the upper limit of the random number.
Program 2: Generating random numbers in a range using random_int() function.
Random number in range (35, 100): 93 Random number in range (10, 30): 28
- Program to generate random string in PHP
- How to generate a random, unique, alphanumeric string in PHP
- How to get random value out of an array in PHP?
- Generating Random String Using PHP
- PHP Program to Find the Number Occurring Odd Number of Times
- PHP Program for nth Catalan Number
- PHP Program for Minimum number of jumps to reach end
- Program to find the number of days between two dates in PHP
- PHP Program to Count number of binary strings without consecutive 1's
- PHP Program to Count trailing zeroes in factorial of a number
- CSS | :out-of-range Selector
- PHP | range() Function
- CSS | :in-range Selector
- HTML | DOM Input Range Object
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.