SoFunction
Updated on 2025-03-10

PHP built-in functions generate random number instances

1. rand function

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

echo rand();           // Generate a random number between 0~RAND_MAX. The value of RAND_MAX is 32767 under Windows system. RAND_MAX can be obtained using the function getandmax()echo rand(1000000, 9999999);   // Generate random numbers between 1000000~9999999$seed = time();          // Use time as seed sourcesrand($seed);           // Sow the random number generator seedsecho rand();           // Generate random numbers between 0~32768 based on seeds.  If the $seed value is fixed, the generated random number will not changeecho rand(1000000, 9999999);   // Generate based on seeds 1000000~9999999 Random number between。if $seed Fixed value,The generated random number will not change

2. mt_rand function

mt_rand() is to use the Mersenne Twister algorithm to return random integers. The main difference from the rand() function is that mt_rand() produces random values ​​at an average speed four times faster than rand() provided by libc, and the seeding function uses mt_srand() instead of srand(). Although there are such differences, their usage methods are still similar, as follows:

echo mt_rand();          // Generate a random number between 0~RAND_MAX. The value of RAND_MAX under Windows system is 2147483647 (different from RAND_MAX in rand()). RAND_MAX can be obtained using the function mt_getrandmax()echo mt_rand(1000000, 9999999);  // Generate random numbers between 1000000~9999999, not affected by system RAND_MAX$seed = time();          // Use time as seed sourcemt_srand($seed);         // Sow the random number generator seedsecho rand();           // Generate random numbers between 0~RAND_MAX according to the seed. If the $seed value is fixed, the generated random numbers will not change.echo rand(1000000, 9999999);   // Generate based on seeds 1000000~9999999 Random number between,if $seed Fixed value,The generated random number will not change

Description: Rand() and mt_rand() are random numbers generated by the functions rand() and mt_rand() are integers and do not contain English letters.

3. Uniqid function

The uniqid() function generates a unique ID based on the current time in microseconds. The length of the default generated ID is 13 or 23 bits, consisting of English letters and numbers. The uniqid() function has two parameters, the format is as follows:

uniqid(prefix,more_entropy)

in,

prefix: generate prefix for ID

more_entropy: whether to add extra entropy

The following program,

echo uniqid();          // Generate 13-bit string, such as: 55f540e273e93echo uniqid('one.');       // Generate a string with prefixed one. Add 13-bit random characters, such as: one.55f540e273e93echo uniqid('two.', true);    // Generate prefix astwo.add23String of random characters(add了熵),like:two.55f540e273e932.77804707,More than above 10 Bit,That's more:2.77804707

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