This article mainly introduces the usage of PHP random function. Interested friends can refer to it. I hope it will be helpful to everyone.
Written in front
The classic usage of PHP syntax features and related function libraries may not really achieve the effect of making a big difference, but mastering these methods can There will be some help in your work and study!
2. Preface
PHP is a common scripting language, mainly because it is easy to learn and quick to use. Almost 50% of Web programs have PHP. (Incomplete statistics). PHP provides a wealth of functions and API interfaces for development, which makes it very convenient for us to use its powerful built-in functions and extensions.
3. PHP random functions
PHP random functions mainly include rand, mt_rand, array_rand, and the randomly "arranged" (shuffled) functions shuffle and str_shuffle , a uniqid that can generate a unique ID.
1. rand generates random numbers:
<?php $base = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; $count = strlen($base); $random = ''; for ($i=0; $i < 16; $i++) { $random.=$base[rand(0,$count-1)]; } echo $random; ?>
rand() function is a random number generator using libc Generating random numbers is generally slow and has 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 = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; $count = strlen($base); $random = ''; for ($i=0; $i < 16; $i++) { $random.=$base[mt_rand(0,$count-1)]; } echo $random; ?>
Many old libc random number generators have some inconsistencies Certain and unknown properties and 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('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'); $random = ''; for ($i=0; $i < 16; $i++) { $random.=$base[array_rand($base)]; } echo $random; ?>
array_rand returns a random key value in the array, somewhat similar to the mt_rand() function , the rest is nothing special, just use it flexibly.
4. shuffle function
<?php $base = 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'); if(shuffle($base)){ print_r($base); } ?>
The shuffle function is to disrupt the sequence of an array, which is a bit random. means, 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 = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; echo str_shuffle($base); ?>
6. uniqid function
<?php echo uniqid(); //54f806528172f ?>
4. Summary
Random function is almost the most basic function of every language, and PHP’s support for random functions is no exception. Here we introduce rand, mt_rand, array_rand, shuffle The basic usage of , str_shuffle, and uniqid functions can be used flexibly in combination with practical applications.
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
php common techniques for error handling
php implements web services Method
php Method of dynamically creating html code based on array
##
The above is the detailed content of How to use PHP random function. For more information, please follow other related articles on the PHP Chinese website!