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

string random_bytes ( int $length )

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

The execution output of the above program is:

6f36d48a29

random_int()

Syntax format

int random_int (int $min, int $max)

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

The execution output of the above program is:

723
-64