random_bytes 


crypto apg

GENERATES cryptographically secure pseudo-random strings.


This function generates a STRING of arbitrary length suitable for generating random data as well as keys or startup vectors.

This function was introduced in PHP 7.0, however, there are implementations available for PHP 5.2 through PHP 5.6.



RANDOM NUMBERS

are numbers that occur in a sequence such that two conditions are met:

1 . the values are uniformly distributed over a defined interval or set.
2 . it is impossible to predict future values based on past or present ones.

Random numbers are important in statistical analysis and probability theory.

This function generates cryptographically secure values, and it should be used for these purposes.

You can see more:

   CryptGenRandom function on Windows  !

   CNG-API on Windows  !

   GETRANDOM(2) on LINUX  !



<?php

string random_bytes 
int $length )


where,

$length The length of the STRING to be returned in bytes

?>

 $length 


The length of the STRING to be returned in bytes.



  1 EXERCISE   

<?php

// Run this code several times

$len01 mt_rand(2040);

$rndbin01 random_bytes($len01);
$lenbin01 strlen($rndbin01);

$rndhex01 bin2hex($rndbin01);
$lenhex01 strlen($rndhex01);

echo 
'RAW-BIN (' $lenbin01 ')<br>' 
          
$rndbin01 '<br><br>HEX (' $lenhex01 ')<br>' 
          
$rndhex01;

?>