What are some methods for generating random numbers in PHP?

php中世界最好的语言
Release: 2023-03-26 15:44:01
Original
2033 people have browsed it

This time I will bring you some methods of generating random numbers in PHP, and what are the precautions for generating random numbers in PHP. The following is a practical case, let's take a look.

The first method uses mt_rand()

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 second method (fastest)

function make_password( $length = 8 ) { // 密码字符集,可任意添加你需要的字符 $chars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l','m', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y','z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L','M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y','Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '!', '@','#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '[', ']', '{', '}', '<', '>', '~', '`', '+', '=', ',', '.', ';', ':', '/', '?', '|'); // 在 $chars 中随机取 $length 个数组元素键名 $keys = array_rand($chars, $length); $password = ''; for($i = 0; $i < $length; $i++) { // 将 $length 个数组元素连接成字符串 $password .= $chars[$keys[$i]]; } return $password; }
Copy after login

The third way is to get the current timestamp

function get_password( $length = 8 ) { $str = substr(md5(time()), 0, $length);//md5加密,time()当前时间戳 return $str; }
Copy after login

The fourth way is to scramble the string

function getrandstr(){ $str='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890'; $randStr = str_shuffle($str);//打乱字符串 $rands= substr($randStr,0,6);//substr(string,start,length);返回字符串的一部分 return $rands; }
Copy after login

//Start creating

Verification code(Generate directly using functions, which is more convenient and fast)

$code = rand(10000, 99999);
Copy after login

php mt_rand compares the effect of generating 0~1 random decimals

lcg_value description

float lcg_value (void)

lcg_value() returns a pseudo-random number in the range (0, 1). This function combines two congruential generators with periods 2^31 - 85 and 2^31 - 249. The period of this function is equal to the product of these two prime numbers.

Return: pseudo-random number in the range (0, 1).

Copy after login
Output:

0.11516515851995

0.064684551575297
0.68275174031189
0.55730746529099
0.70215008878091

Two kinds of generation 0~1 random decimal method for comparison

1. Execution time comparison

The running time of executing 100,000 times based on mt_rand() and mt_getrandmax() algorithms

Copy after login
Output: run time 266.893148 ms

The running time of executing lcg_value() 100,000 times

Copy after login
Output: run time 86.178064 ms

Execution time Compared with the above, because lcg_value() is directly a native PHP method, and mt_rand() and mt_getrandmax() need to call two methods and need to be calculated, so the execution time of lcg_value() is about 3 times faster.

2. Random effect comparison

Random effect based on mt_rand() and mt_getrandmax() algorithms

=0.5){ imagesetpixel($im, $x, $y, $color1); }else{ imagesetpixel($im, $x, $y, $color2); } } } imagepng($im); imagedestroy($im); ?>
Copy after login
Random effect of lcg_value()

=0.5){ imagesetpixel($im, $x, $y, $color1); }else{ imagesetpixel($im, $x, $y, $color2); } } } imagepng($im); imagedestroy($im); ?>
Copy after login
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Alibaba Cloud Win2016 installation Apache and PHP environment tutorial detailed explanation

Operation of PHP associative array and index array Detailed explanation of steps

The above is the detailed content of What are some methods for generating 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!