rand()

rand() — Generate a random integer.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

If called without the optional min, max arguments rand() returns a pseudo-random integer between 0 and getrandmax(). If you want a random number between 5 and 15 (inclusive), for example, use rand(5, 15).

Signature

rand(): int rand(int $min, int $max): int

Parameters

ParameterTypeWhat it takes
$minintThe lowest value to return (default: 0)
$maxintThe highest value to return (default: getrandmax())

Return value

A pseudo random value between min (or 0) and max (or getrandmax(), inclusive).

Example

A runnable snippet:

echo rand(), "\n"; echo rand(), "\n"; echo rand(5, 15), "\n";

Output:

7771 22264 11

Changelog

VersionChange
7.2.0rand() has received a bug fix for a modulo bias bug. This means that sequences generated with a specific seed may differ from PHP 7.1 on 64-bit machines.
7.1.0rand() has been made an alias of mt_rand().

See also

srand(), getrandmax(), mt_rand(), random_int(), random_bytes()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page rand() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.