random_int

(PHP 7, PHP 8)

random_int获取生成加密安全、均匀分布的整数

说明

random_int(int $min, int $max): int

生成加密随机整数,适用于对结果公平至关重要的时候,比如扑克游戏洗牌时。

此函数使用的随机性来源如下:

注意: 虽然此函数是 PHP 7.0 添加到 PHP 中,但是从 PHP 5.2 到 PHP 5.6 都可以用 » 用户级实现

参数

min

要返回的最小值。

max

要返回的最大值。

返回值

从闭合区间 [min, max] 返回加密安全、均匀分布的整数。minmax 都有可能返回。

错误/异常

更新日志

版本 说明
8.2.0 CSPRNG 失败时,此函数现在将抛出 Random\RandomException。之前抛出普通的 Exception

范例

示例 #1 random_int() 示例

<?php
var_dump
(random_int(100, 999));
var_dump(random_int(-1000, 0));
?>

以上例程的输出类似于:

int(248)
int(-898)

参见

add a note

User Contributed Notes

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