PHP mt_rand()Functions

PHP Math 参考手册PHP Math Reference Manual

Examples

Generate random numbers:

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

Run example »

Definition and Usage

The mt_rand() function generates random integers using the Mersenne Twister algorithm.

Tip:This function is a better choice for generating random values, and the speed of returning results isrand()4 times that of the rand() function.

Tip:If you want a random integer between 10 and 100 (inclusive), use mt_rand (10,100).


Syntax

mt_rand();

or

mt_rand(min,max);

Parameter Description
min Optional. Specifies the smallest number to return. Default is 0.
max Optional. Specifies the largest number to return. Default ismt_getrandmax()。

Technical Details

Return value: Betweenmin(or 0) andmax(or mt_getrandmax()) inclusive. Ifmax < minthen returns FALSE.
Return type: Integer
PHP Version: 4+
PHP Changelog: PHP 4.2.0: The random number generator is seeded automatically.
PHP 5.3.4: Ifmax < minthen an E_WARNING is reported and FALSE is returned.


PHP Math 参考手册PHP Math Reference Manual Other Extensions