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

PHP mt_rand() Function

PHP mt_rand() Function

PHP mt_rand() function is used to generate a random integer value between the specified minimum and maximum values. It produces a better random value than the rand() function and also much faster. It accepts two parameters $min and $max that is the maximum and minimum value to be returned. It returns a random integer type number between the specified min (or 0) and max.

Syntax:

mt_rand();

//OR

mt_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 mt_rand() function in PHP:

<html>
<body>

<?php

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

?>

</body>
</html>
Output:
200092891
1146224364
441392858

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

<html>
<body>

<?php

echo mt_rand()."<br>";
echo mt_rand(100,200)."<br>";
echo mt_rand(1000,2000)."<br>";
echo mt_rand(0,100)."<br>";

?>

</body>
</html>
Output:
457619462
127
1871
25