PHP | rand() function


The rand() is an inbuilt-function in PHP used to generate a random number.

Syntax:

rand() 
The rand() function is use to generate a random integer.

To generate a random integer in some range:
Syntax



rand(min,max)
min specifies the lowest value that will be returned.
max specifies the highest value to be returned.

This function will generate a random value in the range [min,max]

Note
If the min and max value is not specified, default is 0 and getrandmax() respectively.

Example

  1. rand() will return a random integer between 0 and getrandmax().
  2. rand(15,35) will return a random integer in the range [15,35].
filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
      
    // Generating a random number
    $randomNumber = rand();
      
    // Print
    print_r($randomNumber);
  
    // New Line
    print_r("\n");
      
    // Generating a random number in a 
    // Specified range.
    $randomNumber = rand(15,35);
      
    // Print
    print_r($randomNumber);
      
?>

chevron_right


Note :
The output of the code may change every time it is run. So the output may not match with the specified output.
Output:

1257424548
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.