php8.5
Home/ Manual/ gmp / functions/ gmp_random_range

gmp_random_range

PHP function Edit on GitHub ✎

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

Get a uniformly selected integer

Description

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.

Cryptographically-insecure

Parameters

min

A GMP number representing the lower bound for the random number

max

A GMP number representing the upper bound for the random number

Return Values

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

Errors/Exceptions

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

Examples

gmp_random_range() example

php
<?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";
?>

The above example will output:

output
42
-67

Source: reference/gmp/functions/gmp-random-range.xml · from the official PHP manual (php/doc-en)