gmp_random_range
(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
.
本函数并不会生成安全加密的值,并且不可用于加密或者要求返回值不可猜测的目的。
如果需要加密安全随机,则可以将 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
用户贡献的备注
备份地址:http://www.lvesu.com/blog/php/function.gmp-random-range.php