PHP can do big things too. Random function _php Techniques

Source: Internet
Author: User
Tags generator rand shuffle unique id

It's written in front.

PHP can also do great things is my summary of PHP syntax features and related functions of the class Library classic usage, it is not necessarily true to achieve 42 of the effectiveness of the dial, but master these methods, you can work and learn some help, I hope we can brainstorm, the "PHP can also do great things" rich more exciting! Reprint please indicate the source (3mc2.com)

Second, preface

PHP is a common scripting language, mainly because it is easy to learn, fast to start, almost 50% of the web programs have PHP figure (incomplete statistics). PHP provides rich functions and API interfaces for development, which makes it very easy to use its powerful built-in functions and extensions, this is the second of the "PHP can do great things" series, mainly summarizing the knowledge of PHP in the random number generator.

Third, PHP random function

PHP random functions are mainly Rand, Mt_rand, Array_rand, and random "arrange" (scrambled order) function shuffle, str_shuffle, can produce a unique ID uniqid.

1. Rand produces 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 uses a libc random number generator to generate random numbers, generally slower, and with uncertainties, and recommends the use of Mt_rand functions 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 produce random number:

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

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

If the optional parameter min and Max,mt_rand () are not 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 + +) { 
 $random. = $base [Array_rand ($base)];
}
Echo $random;

Array_rand returns the random key value in the array, somewhat similar to the Mt_rand () function, and the rest is nothing special and flexible to use.

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, a little random meaning, where it is placed in a random function. The return value is a bool value, which is equivalent to referring directly to the original variable.

5. Str_shuffle function

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

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

6. uniqid function

<?php
Echo uniqid ();
54f806528172f
?>

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

Iv. Summary
random functions are almost the most basic function of each language, PHP for random function support is no exception, here is the rand, Mt_rand, Array_rand, Shuffle, Str_shuffle, uniqid the basic use of functions, More can be applied flexibly with practical application.

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.