An in-depth understanding of PHP random Numbers _php Tutorial

Source: Internet
Author: User
PHP Mt_srand sowing 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 CodeThe code is as follows:
void Mt_srand (int seed)

Use seed to sow the random number generator. Starting with PHP version 4.2.0, the seed parameter becomes optional and is set to a number of times when the item is empty.
Example 1. Mt_srand () example
Copy CodeThe code is as follows:
Code highlighting produced by Actipro Codehighlighter (freeware) http://www.codehighlighter.com/--> 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, it is no longer necessary to use the Srand () or Mt_srand () function to sow the random number generator, which is now done automatically.
See Mt_rand (), Mt_getrandmax (), and Srand ().

PHP Mt_rand () function
Definition and usage
Mt_rand () returns a random integer using the Mersenne Twister algorithm.
Grammar
Copy CodeThe code is as follows:
Mt_rand (Min,max)

description
If no optional parameter min and Max,mt_rand () are 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 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.
Hints and Notes
notes:Since PHP 4.2.0, it is no longer necessary to sow the random number generator with the Srand () or Mt_srand () function, which is now done automatically.
notes:In the previous version of 3.0.7, Max was meant to be range. To get the same random number from the previous example of 5 to 15 in these versions, the short example is Mt_rand (5, 11).
Example
In this case, we will return some random numbers:
Copy CodeThe code is as follows:
Echo (Mt_rand ());
Echo (Mt_rand (10,100));
?>

The output is similar to:
3150906288
513289678
35
Note:The above function gives a random integer, the characters outside the number will not come out, if you want to produce other characters, you need another custom method, as follows:
Copy CodeThe code is as follows:
/*
* $length: The length of the random number string
* $type: Types that generate 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

http://www.bkjia.com/PHPjc/327348.html www.bkjia.com true http://www.bkjia.com/PHPjc/327348.html techarticle PHP Mt_srand sowed 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 ...

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