Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP rand() Function

PHP rand() Function

PHP Math Reference Manual PHP Math Reference Manual

Example

Generate a random number:

"); echo(rand() . "
"); echo(rand(10,100)); ?>

Run Example »


Definition and Usage

The rand() function generates random integers.

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

Tip:mt_rand() This function is a better choice for generating random values, returning results 4 times faster than rand().

Syntax

rand();

or

rand(

min,max

);

Parameters Description
min Optional. Specifies the minimum number to return. Default is 0.
max Optional. Specifies the maximum number to return. Default is getrandmax().

Technical Details

Return Value: Between min(or 0) and max(or mt_getrandmax()), inclusive.
Return Type: Integer
PHP Version: 4+
PHP Changelog: PHP 4.2.0: The random number generator is seeded automatically.

PHP Math Reference Manual PHP Math Reference Manual