random_int()

random_int() — Get a cryptographically secure, uniformly selected integer.

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

Signature

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

Parameters

ParameterTypeWhat it takes
$minintThe lowest value to be returned.
$maxintThe highest value to be returned.

Return value

A cryptographically secure, uniformly selected integer from the closed interval [min, max]. Both min and max are possible return values.

Example

A runnable snippet:

var_dump(random_int(100, 999)); var_dump(random_int(-1000, 0));

Output:

int(248) int(-898)

Changelog

VersionChange
8.2.0In case of a CSPRNG failure, this function will now throw a Random\RandomException. Previously a plain Exception was thrown.

See also

random_bytes()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page random_int() (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.