PHP is also capable of major random functions, PHP event random function _php Tutorial

Source: Internet
Author: User
Tags shuffle

PHP is also capable of major random functions, PHP event random functions


Write in front

PHP can also do big things is my summary of PHP syntax features and related functions of the classic use of library, and is not really able to achieve the effect of 42 dial, but master these methods, you can work and study some help, I hope you can brainstorm, will "PHP can do big things" rich more wonderful! Reprint please specify the source (3mc2.com)

Second, preface

PHP is a common scripting language, mainly because it is easy to learn, quick to get started, almost 50% of the web programs have PHP figure (not fully counted). PHP provides a rich set of functions and API interfaces for development, which makes it easy to use its powerful built-in functions and extensions, and this article is the second in the series "PHP can do great things", which summarizes PHP's knowledge of random number generators.

Third, PHP random function

PHP random functions mainly include Rand, Mt_rand, Array_rand, and random "permutation" (scrambled order) functions shuffle, str_shuffle, capable of generating a unique ID uniqid.

1. Rand generates random numbers:

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

The rand () function is a random number generator using LIBC to generate random numbers, generally slower, and with uncertainties, it is recommended to use the Mt_rand function instead.

The Getrandmax () function returns the maximum number of random numbers that the RAND function can produce (my system is 32767), so do not exceed the Getrandmax return value when setting the second parameter of the RAND function.

2. Mt_rand generates random numbers:

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

Many of the old libc random number generators have some uncertainties and unknown characteristics and are slow. The rand () function of PHP uses the libc random number generator by default. The Mt_rand () function is informally used to replace it. The function uses the known characteristics of the»mersenne Twister as a random number generator, which can produce random values four times times faster than Rand () provided by LIBC. It is strongly recommended that you use the Mt_rand function instead of Rand during the development process.

If no optional parameter min and Max,mt_rand () are provided, the pseudo-random number between 0 and Mt_getrandmax () is returned. For example, want a random number between 3 and 20 (including 3 and 20), with 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 <; $i + +) {  

Array_rand returns the random key values in the array, a bit like the Mt_rand () function, and the rest is nothing special and flexible to use.

4. Shuffle function

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

5. Str_shuffle function

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

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

6. uniqid function

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

Uniqid can produce a unique string, the scope of this application can be quite extensive

Iv. Summary
Random function is almost the basic function of each language, PHP support for random functions is no exception, here is the rand, Mt_rand, Array_rand, Shuffle, Str_shuffle, uniqid function basic usage, More flexible to use in combination with practical applications.

http://www.bkjia.com/PHPjc/984628.html www.bkjia.com true http://www.bkjia.com/PHPjc/984628.html techarticle PHP also capable of big random functions, PHP event random function written in front of PHP also can do big thing is I summed up the PHP syntax features and related functions of the classical usage of the library, and not necessarily really can be real ...

  • 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.