PHP CSPRNG
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