PHP can also handle major random functions. php major random functions _ PHP Tutorial

Source: Internet
Author: User
Tags random shuffle shuffle
PHP can also handle major random functions, and php major random functions. PHP can also handle major random functions. php's major random functions can also be written in front of the previous article. I have summarized the PHP syntax features and the classic usage of related function libraries, it is not necessarily true that PHP can also handle major random functions.

Preface

The main thing PHP can do is to summarize the PHP syntax features and the classic usage of the relevant function libraries. it is not necessarily true that it can achieve the effect of four or two hundred pounds, but to master these methods, it can be helpful in your work and study. I hope you can brainstorm and enrich PHP as a major event! Reprinted please indicate the source (3mc2.com)

II. Preface

PHP is a common scripting language, mainly because it is easy to learn and easy to use. more than 50% of Web programs have PHP (incomplete statistics ). PHP provides a wide range of functions and API interfaces for development, which allows us to easily use its powerful built-in functions and extensions, this article is the second article in the series "PHP can also do things". it mainly summarizes the knowledge of PHP in random number generator.

III. PHP random functions

PHP random functions include rand, mt_rand, array_rand, and random shuffle and str_shuffle functions that can generate uniqid with unique IDs.

1. Random number generated by rand:

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

The rand () function uses the libc random number generator to generate a random number, which is generally slow and has uncertainties. we recommend using the mt_rand function instead.

The getrandmax () function can return the maximum random number that the rand function can generate (my system is 32767). therefore, do not set the second parameter of the rand function to return a super high value of getrandmax.

2. mt_rand generates a random number:

<?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 features and are very slow. The rand () function of PHP uses the libc random number generator by default. The mt_rand () function is informal to replace it. This function uses known features in Mersenne Twister as a random number generator, which can generate random values at an average speed four times faster than the rand () provided by libc. We strongly recommend that you replace rand with the mt_rand function 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 (including 3 and 20), 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 the random key values in the array, which is a bit similar to the mt_rand () function. there is nothing special about the rest 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 used to disrupt the sequence of an array, which is random. it is placed in the random function. The return value is the bool value, which is equivalent to directly referencing the original variable.

5. str_shuffle function

<?php$base = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';echo str_shuffle($base);?>

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

6. uniqid function

<?phpecho uniqid();//54f806528172f?>

Uniqid can generate a unique string, which can be widely used.

IV. Summary
Random functions are the most basic functions in almost every language, and PHP supports random functions. Here we introduce the basic usage of rand, mt_rand, array_rand, shuffle, str_shuffle, and uniqid functions, more can be flexibly used in combination with practical applications.

Random Functions, php major events random functions written in front of PHP can also be a major event is my summary of the PHP syntax features and the classic usage of related function libraries, not necessarily real...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.