PHP rand() Function
The PHP rand() function returns a pseudo-random integer. If it is called without optional min, max arguments then it returns a pseudo-random integer between 0 and getrandmax(). When min and max arguments are used, then it returns a pseudo-random integer between min and max (inclusive).
Syntax
rand(min, max)
Parameters
min |
Optional. Specify the lowest value to return. Default is 0. |
max |
Optional. Specify the highest value to return. Default is getrandmax(). |
Return Value
Returns a pseudo random value between min (or 0) and max (or getrandmax(), inclusive).
Example:
In the example below, rand() function is used to generate pseudo-random values.
<?php //generate 10 pseudo-random value //between 0 and getrandmax() for ($i = 1; $i <= 10; $i++) { echo rand()."\n"; } ?>
The output of the above code will be:
2089010510 540632205 943019554 1854127608 222684842 1623604850 2043804792 183230485 1293588279 1376642470
Example:
Consider one more example where optional parameters are used to generate pseudo-random values in a given range.
<?php //generate 10 pseudo-random value //between 50 and 100 for ($i = 1; $i <= 10; $i++) { echo rand(50, 100)."\n"; } ?>
The output of the above code will be:
87 90 57 70 55 97 75 83 92 96
❮ PHP Math Reference