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
- rand() will return a random integer between 0 and getrandmax().
- rand(15,35) will return a random integer in the range [15,35].
<?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
filter_none
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
Recommended Posts:
- PHP Math Functions (is_nan, pow, sqrt, exp, log, log10, log1p, max, min, getrandmax, rand, mt_rand)
- How to get the function name inside a function in PHP ?
- PHP 5 vs PHP 7
- PHP | Get PHP configuration information using phpinfo()
- PHP | php.ini File Configuration
- How to import config.php file in a PHP script ?
- PHP | imagecreatetruecolor() Function
- PHP | fpassthru( ) Function
- PHP | ImagickDraw getTextAlignment() Function
- PHP | Ds\Sequence last() Function
- PHP | Imagick floodFillPaintImage() Function
- Function to escape regex patterns before applied in PHP
- PHP | array_udiff_uassoc() Function
- PHP | geoip_continent_code_by_name() Function
- PHP | GmagickPixel setcolor() function
- PHP | opendir() Function
- PHP | cal_to_jd() Function
- PHP | stream_get_transports() Function
- PHP | Ds\Deque pop() Function
- PHP | SimpleXMLElement children() Function
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.

