The method for php to generate random numbers is: It can be achieved by using the mt_rand() function, such as [mt_rand(10,100)], which means generating a random integer between 10 and 100. The mt_rand() function is a better choice for generating random values, returning results four times faster than the rand() function.
You can use the mt_rand() function to generate random numbers, which uses the Mersenne Twister algorithm to generate random integers.
Tip: This function is a better choice for generating random values, returning results 4 times faster than the rand() function.
Tip: If you want a random integer between 10 and 100, inclusive, use mt_rand (10,100).
Let’s take a look at the specific code:
function GetRandStr($length){ $str='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; $len=strlen($str)-1; $randstr=''; for($i=0;$i<$length;$i++){ $num=mt_rand(0,$len); $randstr .= $str[$num]; } return $randstr; } $number=GetRandStr(6); echo $number;
The above is the detailed content of How to generate random numbers in php. For more information, please follow other related articles on the PHP Chinese website!