mt_rand - chung-leong/qb GitHub Wiki

mt_rand - Better random value

int mt_rand( int min, int max)

int mt_rand(void)

Many random number generators of older libcs have dubious or unknown characteristics and are slow. By default, PHP uses the libc random number generator with the rand() function. The mt_rand() function is a drop-in replacement for this. It uses a random number generator with known characteristics using the Mersenne Twister, which will produce random numbers four times faster than what the average libc rand() provides.

If called without the optional min, max arguments mt_rand() returns a pseudo-random value the minimum and maximum possible value for the integer type. If you want a random number between 5 and 15 (inclusive), for example, use mt_rand(5, 15).

Parameters:

min - The lowest value to return. It can be a scalar or an array.

max - The highest value to return. It can be a scalar or an array.

Return Values:

A pseudo-random number between min and max when the two parameters are provided. When they are not, the range is determined by the type of the variable to which the return value is assigned. If $a = mt_rand() and $a is uint8, then the pseudo-number returned will be between 0 and 255. If the variable is int32 instead, the range becomes -2,147,483,648 to 2,147,483,647. If min or max is an array, then the return value is also an array, the size of which match the larger or min and max.

The return type and size of mt_rand() is controlled by the context of how the return value is used. If you assign mt_rand() to an array with four elements, four different pseudo-numbers will be returned.

Version

1.0 and above.