random_int() - php 伪随机数产生器
random_int()
版本:php7
(PHP 7)
Generates cryptographically secure pseudo-random integers
说明
random_int(int $min,int $max): intGenerates cryptographic random integers that are suitable for use where unbiased results are critical, such as when shuffling a deck of cards for a poker game.
The sources of randomness used for this function are as follows:
- On Windows,»CryptGenRandom()will always be used. As of PHP 7.2.0,the» CNG-APIwill always be used instead.
- On Linux,the» getrandom(2)syscall will be used if available.
- On other platforms,/dev/urandomwill be used.
- If none of the aforementioned sources are available, then anExceptionwill be thrown.
Note:Although this function was added to PHP in PHP 7.0,a» userland implementationis available for PHP 5.2 to 5.6, inclusive.
参数
$minThe lowest value to be returned, which must bePHP_INT_MIN
or higher.
The highest value to be returned, which must be less than or equal toPHP_INT_MAX
.
返回值
Returns a cryptographically secure random integer in the range$minto$max, inclusive.
错误/异常
- If an appropriate source of randomness cannot be found,anExceptionwill be thrown.
- If invalid parameters are given,aTypeErrorwill be thrown.
- If$maxis less than$min,anErrorwill be thrown.
范例
Example #1random_int()example
以上例程的输出类似于:
int(248) int(-898)
参见
random_bytes()
Generates cryptographically secure pseudo-random bytes
Here is a simple backporting function, it works for PHP >= 5.1 Randomness test
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!