PHP mt_rand() Function

Definition and Usage

mt_rand() returns a random integer using the Mersenne Twister algorithm.

Syntax

mt_rand(min,max)

Description

If no optional parameters are provided min and max, mt_rand() returns pseudo-random numbers between 0 and RAND_MAX. For example, to get random numbers between 5 and 15 (including 5 and 15), use mt_rand(5, 15).

Many old libc random number generators have some uncertain and unknown characteristics and are slow. PHP's rand() function uses the libc random number generator by default. The mt_rand() function is informally used to replace it. This function uses the known characteristics of the Mersenne Twister as a random number generator and can produce random numbers at an average speed four times faster than the rand() provided by libc.

Tips and Notes

Note:Starting from PHP 4.2.0, it is no longer necessary to use srand() or mt_srand() The function seeds the random number generator, which is now automatically completed.

Note:In versions prior to 3.0.7, the meaning of max is range. To get the same random numbers from 5 to 15 as in the previous example, a brief example is mt_rand(5, 11).

Example

In this example, we will return some random numbers:

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

Output similar to:

3150906288
513289678
35