How to generate random numbers in php

王林
Release: 2023-03-01 17:36:01
Original
8681 people have browsed it

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;
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template