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:
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:
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