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
- 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 call a function that return another function in JavaScript ?
- How to get the function name inside a function in PHP ?
- How to get the function name from within that function using JavaScript ?
- PHP | max( ) Function
- p5.js | box() Function
- PHP | min( ) Function
- CSS | rgb() Function
- D3.js | d3.map.set() Function
- p5.js | hex() function
- D3.js | d3.rgb() Function
- p5.js | str() function
- PHP | dir() Function
- PHP | Ds\Map get() Function
- PHP | pos() 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.



