Base de conocimientosCentro de exámenes
ESEspañol
Elegir idioma13

CKTIS / PHP

random_int()

Obtiene un entero criptográficamente seguro con distribución uniforme entre los límites inclusivos.

Versión: PHP 8.4; repository runtime, production runtime not attested

Sintaxis

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

Obtiene un entero criptográficamente seguro con distribución uniforme entre los límites inclusivos. Límites iguales devuelven ese valor. Un intervalo invertido produce ValueError y un fallo de la fuente en PHP 8.4 produce Random\RandomException.

Firma

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

Salida del ejemplo

[true,4]
ValueError

Ejemplos

<?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); }

Resultado

[true,4]
ValueError

Fuentes