CakeFest 2024: The Official CakePHP Conference

gmp_random_bits

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

gmp_random_bitsRandom number

说明

gmp_random_bits(int $bits): GMP

Generate a random number. The number will be between 0 and 2$bits - 1.

bits must greater than 0, and the maximum value is restricted by available memory.

警告

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

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

参数

bits

The number of bits to generate.

返回值

A random GMP number.

错误/异常

If bits is less than 1, a ValueError will be thrown.

示例

示例 #1 gmp_random_bits() example

<?php
$rand1
= gmp_random_bits(3); // random number from 0 to 7
$rand2 = gmp_random_bits(5); // random number from 0 to 31

echo gmp_strval($rand1) . "\n";
echo
gmp_strval($rand2) . "\n";
?>

以上示例会输出:

3
15

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top