(PHP 5 >= 5.6.3, PHP 7, PHP 8)
gmp_random_range — Get a uniformly selected integer
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.
Questa funzione non genera valori crittograficamente sicuri, e non deve essere usata per scopi crittografici, o per scopi che richiedono che i valori restituiti non siano indovinabili.
Se è necessaria una casualità crittograficamente sicura, è possibile usare Random\Randomizer con il motore Random\Engine\Secure. Per casi d'uso semplici, le funzioni random_int() e random_bytes() forniscono un'API comoda e sicura basata sul CSPRNG del sistema operativo.
minA GMP number representing the lower bound for the random number
maxA GMP number representing the upper bound for the random number
Returns a GMP object which contains
a uniformly selected integer from the closed interval
[min, max]. Both
min and max are
possible return values.
If max is less than min,
a ValueError will be thrown.
Example #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";
?>Il precedente esempio visualizzerà:
42 -67