PHP | random_int()函数

这个 随机整() 是PHP中的内置函数。其主要功能是生成加密安全的伪随机整数值。当临界条件下出现无偏结果时,则使用生成的加密随机整数。 该函数中使用的不同随机性来源如下所示:-

null
  • 窗口: 使用CryptGenRandom()函数。
  • Linux: getrandom(2)要使用的系统调用函数。

语法:

int random_int ( $min, $max )

参数:

  • $min: 返回 最低值 ,等于PHP_INT_MIN或更高。
  • $max: 返回 最高值 ,小于或等于PHP_INT_MAX。

返回值: 一个加密安全的随机整数,范围从最小到最大(含最小值)。 例如:

Input : min= 10, max=10000Output : int(5183)Input : min= -244441, max= 1Output : int(-60209)

下面的程序演示了PHP中的random_int()函数。 项目1:

php

<?php
// PHP program to demonstrate
// the random_int() function
// given min and max range
var_dump(random_int(1, 5555555));
var_dump(random_int(-1, 100000));
var_dump(random_int(9, 10));
?>


输出

int(835427)int(86695)int(10)

下面的程序演示了PHP中的random_int()函数。 项目2: 当写入无效范围时,则会出现运行时错误。

php

<?php
// PHP program to demonstrate
// the random_int() function
// given min and max range
// Not valid range
$t = (random_int(99, 11));
// Print result
echo "(int)", $t ;
?>


输出

Runtime Error

异常错误:

  • 提供的参数无效 打字错误 .
  • 字节长度无效 错误 .
  • 如果未找到随机性的来源,则将抛出异常。

参考资料: http://php.net/manual/en/function.random-int.php

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享