PHP rand() Function

Definition and Usage

The rand() function returns a random integer.

Syntax

rand(min,max)
Parameter Description
min,max Optional. Specifies the range of random numbers generated.

Description

If optional parameters are not provided min and max, rand() returns a pseudo-random integer between 0 and RAND_MAX. For example, to get a random number between 5 and 15 (including 5 and 15), use rand(5, 15).

Tips and Notes

Note:On some platforms (such as Windows), RAND_MAX is only 32768. If the required range is greater than 32768, then specifying the min and max parameters can generate numbers greater than RAND_MAX, or consider using mt_rand() instead.

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 done.

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

Example

This example will return some random numbers:

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

Output:

17757
3794
97