array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'zh', ), 'this' => array ( 0 => 'function.gmp-random-range.php', 1 => 'gmp_random_range', ), 'up' => array ( 0 => 'ref.gmp.php', 1 => 'GMP 函数', ), 'prev' => array ( 0 => 'function.gmp-random-bits.php', 1 => 'gmp_random_bits', ), 'next' => array ( 0 => 'function.gmp-random-seed.php', 1 => 'gmp_random_seed', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'reference/gmp/functions/gmp-random-range.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(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