CKTIS / PHP
random_int()
Draw a uniformly distributed, cryptographically secure integer from the inclusive interval.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
random_int(int $min, int $max): intDraw a uniformly distributed, cryptographically secure integer from the inclusive interval. Equal endpoints give that value. A minimum greater than the maximum raises ValueError. A failure of the randomness source raises Random\RandomException on PHP 8.4.
Signature
random_int(int $min, int $max): int
Example output
[true,4]
ValueError
Examples
<?php
$roll=random_int(1,6);
echo json_encode([$roll>=1 && $roll<=6,random_int(4,4)]), "\n";
try { random_int(2,1); } catch (ValueError $e) { echo get_class($e); }
Result
[true,4] ValueError