A deep understanding _php example based on PHP random number

Source: Internet
Author: User
Tags generator rand
PHP Mt_srand to sow a better random number generator seed
Mt_srand
(PHP 3 >= 3.0.6, PHP 4, PHP 5)
Mt_srand--Sowing a better random number generator seed
description
Copy Code code as follows:

void Mt_srand (int seed)

Seed is used to sow the random number generator. Starting with the PHP 4.2.0 version, the seed parameter becomes optional, and when the entry is empty, it is set to a number of times.
Example 1. Mt_srand () example
Copy Code code as follows:

Code highlighting produced by Actipro Codehighlighter (freeware) http://www.CodeHighlighter.com/--><?php
Seed with microseconds
function Make_seed ()
{
List ($usec, $sec) = Explode (', microtime ());
return (float) $sec + ((float) $usec * 100000);
}
Mt_srand (Make_seed ());
$randval = Mt_rand ();
?>

Note: since PHP 4.2.0, no longer need to use the Srand () or Mt_srand () function for the random number generator seeding, has been automatically completed.
See Mt_rand (), Mt_getrandmax () and Srand ().

PHP Mt_rand () function
Definitions and usage
Mt_rand () returns a random integer using the Mersenne twister algorithm.
Grammar
Copy Code code as follows:

Mt_rand (Min,max)

description
If the optional parameter min and Max,mt_rand () are not provided, the pseudo random number between 0 and Rand_max is returned. For example, want a random number between 5 and 15 (including 5 and 15), with Mt_rand (5, 15).
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.
Tips and comments
Note:Since PHP 4.2.0, you no longer need to use the Srand () or Mt_srand () function to sow the random number generator, which is now automatically completed.
Note:In the previous version of 3.0.7, Max meant a range. To get a random number in these versions and the same 5 to 15 as in the previous example, the short example is Mt_rand (5, 11).
Example
In this case, we'll return some random numbers:
Copy Code code as follows:

<?phpecho (Mt_rand ());
Echo (Mt_rand ());
Echo (Mt_rand (10,100));
?>

The output is similar:
3150906288
513289678
35
Note:The above function gives the random integer, the character outside the number is not come out, if you want to produce other characters, you need another custom method, as follows:
Copy Code code as follows:

<?php
/*
* $length: Length of random number string
* $type: Types that produce random numbers
* */
function random ($length, $type = "") {
$chars = $type? "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ": "0123456789abcdef";
$max = strlen ($chars)-1;
Mt_srand (Double) microtime () * 1000000);
for ($i = 0; $i < $length; $i + +) {
$string. = $chars [Mt_rand (0, $max)];
}
return $string;
}
$var =random (' haha ');
Echo ($var);
?>

Output:
Fe61e294e5f46437cb3a92b92643ead6

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.