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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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;
  
?>

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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;
  
?>

chevron_right


Output:

Random number in range (35, 100): 93
Random number in range (10, 30): 28


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.