Name

mt_rand()

Synopsis

    int mt_rand ( [int min, int max] )

The mt_rand() function returns random numbers, similar to the rand(). However, it uses the Mersenne Twister algorithm to generate "better" random numbers (i.e., more random), and is often preferred.

If you supply no parameters, mt_rand() will return a number between 0 and mt_getrandmax(). If you supply it with two parameters, mt_getrandmax() will use those as the upper and lower limits for the random number it generates. The limits are inclusive: if you specify 1 and 3, your random number could be 1, 2, or 3.

    $mtrand = mt_rand();
    $mtrandrange = mt_rand(1,100);

The maximum value that can be generated by mt_rand() varies depending on the system you use, but on both Windows and Unix, the default is 2,147,483,647.

Get PHP in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.