Home >>PHP Math Functions >PHP rand() Function

PHP rand() Function

PHP rand() Function

PHP rand() function is used to generates a random integer. It randomly generates a valid integer value whenever it is called. If we want to generate a random number between any specified value then we can pass the values as $min and $max parameter. Mt_rand() function is also used to generate a random value like it but mt_rand() is 4 times faster than the rand() function.

Syntax:

rand();

or

rand($min,$max);

Parameter Values

Parameter Description
min This is an optional parameter. This parameter defines the lowest number to be returned.
max This is an optional parameter. This parameter defines the highest number to be returned.

Here is an example of rand() function in PHP:

<html>
<body>

<?php

echo rand()."<br>";
echo rand()."<br>";
echo rand()."<br>";
echo rand()."<br>";

?>

</body>
</html>
Output:
609792864
736712377
1865713737
1001483524

Here is another example of rand() function in PHP:

<html>
<body>

<?php

echo rand()."<br>";
echo rand(0,10)."<br>";
echo rand(10,100)."<br>";
echo rand(100,1000)."<br>";
echo rand(1,9)."<br>";

?>

</body>
</html>
Output:
570414555
7
24
866
6