random_int
(PHP 7, PHP 8)
Get a cryptographically secure, uniformly selected integer
Description
Generates a uniformly selected integer between the given minimum and maximum.
The randomness generated by this function is suitable for all applications, including the generation of long-term secrets, such as encryption keys.
Sources Backport
Parameters
minThe lowest value to be returned.
maxThe highest value to be returned.
Return Values
A cryptographically secure, uniformly selected integer from the closed interval [min, max]. Both min and max are possible return values.
Errors/Exceptions
- If
maxis less thanmin, aValueErrorwill be thrown.
Changelog
| Version | Description |
|---|---|
| 8.2.0 | In case of a CSPRNG failure, this function will now throw a Random\RandomException. Previously a plain Exception was thrown. |
Examples
random_int() example
<?php
var_dump(random_int(100, 999));
var_dump(random_int(-1000, 0));
?>The above example will output something similar to:
int(248)
int(-898)See Also
Random\Randomizer::getIntrandom_bytes()