Home  >  Article  >  Backend Development  >  How to generate random numbers in php

How to generate random numbers in php

王林
王林Original
2020-06-29 10:42:408526browse

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.

How to generate random numbers in php

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What does JWT do?Next article:What does JWT do?