PHP Built in Function Generates Random Number Instances

  • 2021-11-14 05:07:52
  • OfStack

1. rand function

The rand () function can generate random integers without adding any arguments. If you want to set the random number range, you can set the values of min and max in the function. If you need to generate seeds of random numbers, use srand function configuration.


echo rand();           //  Generate  0~RAND_MAX  Random numbers between, Windows  Under the system  RAND_MAX  The value of is  32767 , RAND_MAX  You can use the function  getrandmax()  Obtain 
echo rand(1000000, 9999999);   //  Generate  1000000~9999999  Random number between 
$seed = time();          //  Use time as seed source 
srand($seed);           //  Sow the seeds of random number generator 
echo rand();           //  According to seed generation  0~32768  Random number between. If  $seed  If the value is fixed, the generated random number will not change 
echo rand(1000000, 9999999);   //  According to seed generation  1000000~9999999  Random number between. If  $seed  If the value is fixed, the generated random number will not change 

2. mt_rand function

mt_rand () uses the Mersenne Twister algorithm to return random integers. The main difference from rand () function is that mt_rand () produces random values four times faster on average than rand () provided by libc, and the seeding function uses mt_srand () instead of srand (). Despite this difference, their usage is similar, as follows:


echo mt_rand();          //  Generate  0~RAND_MAX  Random numbers between, Windows  Under the system  RAND_MAX  The value of is  2147483647 (With rand() In  RAND_MAX  Different), RAND_MAX  You can use the function  mt_getrandmax()  Obtain 
echo mt_rand(1000000, 9999999);  //  Generate  1000000~9999999  Between random numbers, independent of the system  RAND_MAX  Influence 
$seed = time();          //  Use time as seed source 
mt_srand($seed);         //  Sow the seeds of random number generator 
echo rand();           //  According to seed generation  0~RAND_MAX  Random number between, if  $seed  If the value is fixed, the generated random number will not change 
echo rand(1000000, 9999999);   //  According to seed generation  1000000~9999999  Random number between, if  $seed  If the value is fixed, the generated random number will not change 

rand () and mt_rand () two functions generated by the random numbers are integers, will not contain English letters.

3. uniqid function

The uniqid () function generates a 1-only ID based on the current time in microseconds. By default, the length of ID is 13 or 23 bits, which is composed of English letters and numbers. The uniqid () function takes two arguments in the following format:


uniqid(prefix,more_entropy)

Among them,

prefix: Generate the prefix of ID

more_entropy: Add additional entropy

The following procedure,


echo uniqid();          //  Generate 13 Bit strings, such as: 55f540e273e93
echo uniqid('one.');       //  Generate a prefix of one. Plus 13 A string of random characters, such as: one.55f540e273e93
echo uniqid('two.', true);    //  Generate a prefix of two. Plus 23 A string of random characters (entropy added), such as: two.55f540e273e932.77804707 , much more than the one above  10  Bits, that is, more: 2.77804707

Explanation: The ID generated by this function is not optimal because it is based on system time. To generate an absolutely 1-only ID, use the md5 () function.


Related articles: