gmp_random_range

(PHP 5 >= 5.6.3, PHP 7, PHP 8)

gmp_random_rangeGet a uniformly selected integer

Descripción

gmp_random_range(GMP|int|string $min, GMP|int|string $max): GMP

Generate a random number. The number will be between min and max.

min and max can both be negative, but min must always be less than max.

Precaución

Esta función no genera valores criptográficamente seguros y no debe ser utilizada para fines criptográficos o fines que requieran que los valores devueltos sean impredecibles.

Si se requiere aleatoriedad criptográficamente segura, se puede utilizar el Random\Randomizer con el motor Random\Engine\Secure. Para casos de uso simples, las funciones random_int() y random_bytes() proporcionan una API conveniente y segura respaldada por el CSPRNG del sistema operativo.

Parámetros

min

A GMP number representing the lower bound for the random number

max

A GMP number representing the upper bound for the random number

Valores devueltos

Returns a GMP object which contains a uniformly selected integer from the closed interval [min, max]. Both min and max are possible return values.

Errores/Excepciones

If max is less than min, a ValueError will be thrown.

Ejemplos

Ejemplo #1 gmp_random_range() example

<?php
$rand1
= gmp_random_range(0, 100); // random number between 0 and 100
$rand2 = gmp_random_range(-100, -10); // random number between -100 and -10

echo gmp_strval($rand1) . "\n";
echo
gmp_strval($rand2) . "\n";
?>

El resultado del ejemplo sería:

42
-67

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top