Two ways to generate random numbers in PHP

墨辰丷
Release: 2023-03-26 07:12:02
Original
1669 people have browsed it

This article mainly introduces two methods of randomly generating numbers in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.

The first method uses mt_rand():

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

Related recommendations:

Detailed explanation of JS randomly generated numbers and sequence methods

Instance of using ip proxy pool to randomly generate IP in python3 requests

Randomly generated addition and subtraction algorithm method Verification code

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!