Deep understanding of random Numbers based on php

  • 2020-06-03 06:11:22
  • OfStack

php mt_srand seeds a better random number generator
mt_srand
(PHP 3 > = 3.0.6, PHP 4, PHP 5)
mt_srand - Seed a better random number generator
instructions

void mt_srand ( int seed ) 

seed was used to seed the random number generator. Starting with PHP 4.2.0, the seed parameter becomes optional and is set to any time when the item is empty.
Example 1. Example mt_srand()

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: Starting from PHP 4.2.0, it is no longer necessary to seed the random number generator with the srand() or mt_srand() functions. It is now automatic.
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

mt_rand(min,max) 

instructions
If the optional parameters min and max are not provided, mt_rand() returns a pseudo-random number between 0 and RAND_MAX. For example, if you want a random number between 5 and 15 (including 5 and 15), use mt_rand(5, 15).
Many of the older libc random number generators have some uncertain and unknown properties and are slow. The rand() function of PHP USES libc random number generator by default. The mt_rand() function is informally used to replace it. This function USES the characteristics known in Mersenne Twister as a random number generator, which can generate random values at an average speed four times faster than rand() provided by libc.
Hints and comments
Note: Starting with PHP 4.2.0, it is no longer necessary to seed the random number generator with the srand() or mt_srand() functions, which are now automatic.
Note: In versions prior to 3.0.7, max means range. To get the same random Numbers of 5 to 15 in these versions, the short example is mt_rand (5, 11).
example
In this case, we will return 1 random number:

<?phpecho(mt_rand());
echo(mt_rand());
echo(mt_rand(10,100));
?>

Output is similar to:
3150906288
513289678
35
Note: For the random integer given by the above function, characters other than Numbers will not come out. If you want to generate other characters, you need another custom method, as shown below:

<?php
/*
 * $length: The length of the random number string 
 * $type: The type that generates 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(32,'haha');
echo($var);
?>

Output:
fe61e294e5f46437cb3a92b92643ead6

Related articles: