Home  >  Article  >  Backend Development  >  PHP generates random numbers mt_rand() rand() mt_srand() function_PHP tutorial

PHP generates random numbers mt_rand() rand() mt_srand() function_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:54:591029browse

mt_rand() returns a random integer using the mersenne twister algorithm.

Grammar
mt_rand(min,max) description
If the optional arguments min and max are not provided, mt_rand() returns a pseudorandom number between 0 and rand_max. For example, if you want a random number between 5 and 15 (inclusive), use mt_rand(5, 15).

*/
echo mt_rand(); //Generate random numbers
echo "
";
echo mt_rand(); //Generate random numbers
echo "
";
echo mt_rand(10,100); //Generate a random number between 10~00

/*
mt_srand() seeds a mersenne twister random number generator.

Grammar
mt_srand(seed) parameter description
seed is required. Use seed to seed the random number generator.

Description
Starting from php tutorial version 4.2.0, the seed parameter becomes optional. When this item is empty, it will be set to a constant number.

*/

function make_seed() //Generate a random number seed
{
List($usec,$sec)=explode(' ',microtime()); //Split the current number of milliseconds
Return(float) $sec+((float)$usec*100000); //Return value
}
mt_srand(make_seed());                                                                                                                                                                                                    $randval=mt_rand(); //Generate random numbers
return
/*

The rand() function returns a random integer.


Grammar

rand(min,max) parameter description

min,max are optional. Specifies the range of random number generation.

Description

If the optional arguments min and max are not provided, rand() returns a pseudorandom integer between 0 and rand_max. For example, if you want a random number between 5 and 15, inclusive, use rand(5, 15).


*/

echo rand(); //Generate random numbers

echo "
";

echo rand(); //Generate random numbers
echo "
";
echo rand(5,15); //Generate a random number between 5~15

/*

Note: On some platforms (e.g. windows) rand_max is only 32768. If the required range is greater than 32768, then specifying the min and max parameters can generate a number greater than rand_max, or consider using mt_rand() instead

*/

http://www.bkjia.com/PHPjc/631709.html

truehttp: //www.bkjia.com/PHPjc/631709.htmlTechArticlemt_rand() returns a random integer using the mersenne twister algorithm. Syntax mt_rand(min,max) Description If the optional parameters min and max are not provided, mt_rand() returns a pseudo-random value between 0 and rand_max...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn