PHP CSPRNG
百度 现场指挥员立即组织安全警戒组立即划定警戒区域,拉起警戒线;侦检组立即进行现场侦查,发现车头与槽车连接处自带液化天燃气罐(用于车辆行驶驱动)变形,有泄漏,槽车槽罐完好,现场指挥员立即下令出2支水枪进行稀释。CSPRNG (Cryptographically Secure Pseudo-Random Number Generator, pseudo-random number generator).
PHP 7 provides a simple mechanism for generating cryptographically strong random numbers by introducing several CSPRNG functions.
random_bytes() - Cryptographically protected pseudo-random string.
random_int() - Cryptographically protected pseudo-random integer.
random_bytes()
Syntax format
Parameters
#length - The number of bytes returned by the random string.
Return value
Returns a string and accepts an int type input parameter representing the number of bytes of the returned result.
Example
<?php $bytes = random_bytes(5); print(bin2hex($bytes)); ?>
The execution output of the above program is:
random_int()
Syntax format
Parameters
min - The minimum value returned, must be greater than or equal to PHP_INT_MIN.
max - The maximum value returned, must be less than or equal to PHP_INT_MAX .
Return value
Returns an int number within the specified range
Example
<?php print(random_int(100, 999)); print(PHP_EOL); print(random_int(-1000, 0)); ?>
The execution output of the above program is:
-64