Home > Article > Backend Development > Two ways to generate random numbers in php
In order to create some contingency in php
, we may need to set the values of some variables to random. In order to solve this problem, php
has built-in functions rand()
and mt_rand()
functions to solve this problem.
First let’s take a look at the syntax of the mt_rand()
function:
mt_rand ( int $min , int $max )
$min: Optional, the minimum value of the generated number
$max: Optional, the maximum value of the generated number
Return value: If there is no minimum value and maximum value, a random value will be returned The maximum possible value of a number, if any, between the minimum and maximum values.
Code example:
<?php echo mt_rand(); echo "<br>"; echo mt_rand(10,100); ?>
输出: 1144620466 //随机不一定相同 12 //随机不一定相同
Note: mt_rand()
generates random values faster on average than rand()
Four times.
Recommended: 《2021 PHP interview questions summary (collection)》《php video Tutorial》
The above is the detailed content of Two ways to generate random numbers in php. For more information, please follow other related articles on the PHP Chinese website!