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().
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 = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; $count = strlen($base); $random = ''; 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 = '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 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('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, 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('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. 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); ?>
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!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Zend Studio 13.0.1
Powerful PHP integrated development environment

Notepad++7.3.1
Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.