PHP program to Generate the random number in the given range (min, max)
Last Updated :
11 Jul, 2025
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
// 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;
?>
Output:
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
// 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;
?>
Output:
Random number in range (35, 100): 93
Random number in range (10, 30): 28
Similar Reads
How to Find the Smallest of given Two Nnumbers In PHP ? Given two numbers num1 and num2, the task is to find the smallest number between them. Here, we will see the different approaches to solving this problem. Examples:Input: 10 5Ouput: 5Table of Content Using Conditional (Ternary) OperatorUsing if-else StatementsUsing the min FunctionThe sort() MethodU
3 min read
How to Randomize the Order of an Array in PHP ? Randomizing the order of an array is a common operation in PHP, especially when dealing with datasets or when you want to shuffle the presentation of items. In this article, we will explore various approaches to randomize the order of an array.Table of ContentUsing shuffle() FunctionUsing usort() Fu
3 min read
How to generate simple random password from a given string using PHP ? In this article, we will see how to generate a random password using the given string. We have given a string and the task is to generate a random password from it. Example: Input: abgADKL123 Output: abgADKL123 Input: geeksforgeeks Output: egksegsfroeke To achieve this, we use the following approach
3 min read
PHP Program to Calculate the Permutation nPr This article will show you how to calculate the Permutation nPr using PHP. Permutations refer to the different ways in which a set of items can be arranged. In mathematics, the number of permutations of 'n' distinct items taken 'r' at a time is denoted as nPr. Calculating nPr, or permutations means
2 min read
Generating OTP (One time Password) in PHP Now these days, OTP (one time password) is mandatory in almost each and every service. A developer can generate OTP in many ways but the challenge is not to be predictive as any one can predict the OTP and can exploit the service. Some of popular format of OTPs are: 4 or 6 digit Numeric OTP. 4 or 6
2 min read
How to generate a random, unique, alphanumeric string in PHP There are many ways to generate a random, unique, alphanumeric string in PHP which are given below: Table of ContentUsing str_shuffle() FunctionUsing md5() FunctionUsing sha1() FunctionUsing random_bytes() FunctionUsing random_int() in a Custom FunctionUsing uniqid() with more_entropy parameterUsing
4 min read