The use of PHP random functions

Source: Internet
Author: User
Tags shuffle
This article mainly introduces the use of PHP random functions, interested in the reference of friends, I hope to be helpful to everyone.

Write in front

The classic usage of PHP syntax features and related function libraries is not necessarily the real ability to achieve 42 of the power, but mastering these methods can help you in your work and study!

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 range of functions and API interfaces for development, which makes it very easy to use its powerful built-in functions and extensions.

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 + +) {  $random. = $base [Array_rand ($base)];} Echo $random;? >

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

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

Summary : The above is the entire content of this article, I hope to be able to help you learn.

Related recommendations:

Common tips for error handling in PHP

How PHP Implements Web services

How PHP dynamically creates HTML code based on an array

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.