PHP 8.4.0 RC4 available for testing

gmp_random_range

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

gmp_random_rangeGet a uniformly selected integer

说明

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.

警告

本函数并不会生成安全加密的值,并且不可用于加密或者要求返回值不可猜测的目的。

如果需要加密安全随机,则可以将 Random\Engine\Secure 引擎用于 Random\Randomizer。对于简单的用例,random_int()random_bytes() 函数提供了操作系统的 CSPRNG 支持的方便且安全的 API

参数

min

A GMP number representing the lower bound for the random number

max

A 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.

示例

示例 #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";
?>

以上示例会输出:

42
-67

添加备注

用户贡献的备注

此页面尚无用户贡献的备注。
To Top