Home  >  Article  >  Backend Development  >  What are the functions that implement randomness in PHP?

What are the functions that implement randomness in PHP?

青灯夜游
青灯夜游Original
2021-09-30 13:56:266871browse

php functions to implement randomness are: 1. rand(), which can generate random integers; 2. mt_rand(); 3. array_rand(), which can return a random key name in the array; 4. shuffle( ), can randomly shuffle the array; 5. str_shuffle(); 6. uniqid().

What are the functions that implement randomness in PHP?

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

PHP random functions mainly include rand, mt_rand, array_rand , there are also randomly "arranged" (shuffled) functions shuffle and str_shuffle, which can generate uniqid with unique IDs.

1. rand generates random numbers

<?php
$base = &#39;abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789&#39;;
$count = strlen($base);
$random = &#39;&#39;;
for ($i=0; $i < 16; $i++) { 
 $random.=$base[rand(0,$count-1)];
}
echo $random;
?>

rand() function uses the random number generator of libc to generate random numbers. It is generally slow and uncertain. factors, it is recommended to use the mt_rand function instead.

The getrandmax() function can return the maximum random number that the rand function can generate (my system is 32767), so when setting the second parameter of the rand function, do not exceed the return value of getrandmax.

2. mt_rand generates random numbers

<?php
$base = &#39;abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789&#39;;
$count = strlen($base);
$random = &#39;&#39;;
for ($i=0; $i < 16; $i++) { 
 $random.=$base[mt_rand(0,$count-1)];
}
echo $random;
?>

Many old libc random number generators have some uncertain and unknown characteristics and are very slow. PHP's rand() function uses the libc random number generator by default. The mt_rand() function is informally used to replace it. This function uses the known features of » Mersenne Twister as a random number generator, which can generate random values ​​on average four times faster than rand() provided by libc. It is strongly recommended to use the mt_rand function instead of rand during development.

If the optional parameters min and max are not provided, mt_rand() returns a pseudo-random number between 0 and mt_getrandmax(). For example, if you want a random number between 3 and 20 (inclusive), use mt_rand(3, 20).

3. array_rand function

<?php
$base = 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;);
$random = &#39;&#39;;
for ($i=0; $i < 16; $i++) { 
 $random.=$base[array_rand($base)];
}
echo $random;
?>

array_rand returns a random key value in the array, which is somewhat similar to the mt_rand() function. The rest is nothing special and can be used flexibly.

4. shuffle function

<?php
$base = 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;);
if(shuffle($base)){
 print_r($base);
}
?>

The shuffle function is to disrupt the sequence of an array, which is a bit random. It is placed in the random function here. The return value is a bool value, which is equivalent to directly referencing the original variable.

5. str_shuffle function

<?php
$base = &#39;abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789&#39;;
echo str_shuffle($base);
?>

The function of str_shuffle here is similar to the function of shuffle. The only difference is the return value. The original string of str_shuffle remains unchanged. of.

6. uniqid function

<?php
echo uniqid();
//54f806528172f
?>

uniqid can generate a unique string, and the scope of this application can be quite wide

Recommended learning: "PHP video tutorial

The above is the detailed content of What are the functions that implement randomness 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