PHP program to Generate the random number in the given range (min, max)
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.
Syntax:
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.
<?php // PHP program to generate random number // in given range // Generating Random numbers without range // rand() function return the random number $num1 = rand(); echo "Random number: " . $num1 . "\n"; // Generating Random numbers in given range $num2 = rand(7, 100); echo "Random number in range (7, 100): ", $num2; ?> |
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.
Syntax:
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.
<?php // PHP program to generate random number // in given range // Generating Random numbers in given range // using random_int() function $num1 = random_int(35, 100); echo "Random number in range (35, 100): " . $num1 . "\n"; // Random number in range (10, 30) $num2 = random_int(10, 30); echo "Random number in range (10, 30): " . $num2; ?> |
Random number in range (35, 100): 93 Random number in range (10, 30): 28
Recommended Posts:
- How to generate random number in given range using JavaScript?
- Program to generate random string in PHP
- How to generate a random, unique, alphanumeric string in PHP
- Generate random alpha-numeric string in JavaScript
- JavaScript | Program to generate one-time password (OTP)
- PHP Program to Find the Number Occurring Odd Number of Times
- JavaScript | Random
- How to get random value out of an array in PHP?
- p5.js | random() Function
- Generating Random String Using PHP
- 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
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.



