Home  >  Article  >  Backend Development  >  Detailed explanation of pseudo-random numbers and true random numbers in PHP

Detailed explanation of pseudo-random numbers and true random numbers in PHP

怪我咯
怪我咯Original
2017-07-11 11:41:352217browse

This article mainly introduces the detailed explanation of pseudo-random numbers and true random numbers in PHP. This article first explains the related concepts of true random numbers and pseudo-random numbers, and gives the comparison with mt_rand()functionAn example code to generate better pseudo-random numbers, friends who need it can refer to it

First of all, it needs to be stated that the computer will not generate absolutely random random numbers, the computer can only generate "pseudo-random numbers" . In fact, absolutely random numbers are just ideal random numbers. No matter how the computer develops, it will not generate a string of absolutely random numbers. Computers can only generate relatively random numbers, that is, pseudo-random numbers.

Pseudo-random numbers are not pseudo-random numbers. The "pseudo" here means regular, that is, the pseudo-random numbers generated by the computer are both random and regular. How to understand it? The generated pseudo-random numbers sometimes follow certain rules, and sometimes they do not follow any rules; some of the pseudo-random numbers follow certain rules; the other part does not follow any rules. For example, "There are no two leaves with the same shape in the world." This points to the characteristics of things, that is, randomness, but the leaves of every tree have similar shapes, which is the commonality of things, that is, regularity. From this perspective, you will probably accept the fact that computers can only generate pseudo-random numbers but cannot generate absolutely random numbers.

First, let’s understand the concepts of true random numbers and pseudo-random numbers.

True random number generators: English: true random number generators, abbreviated as: TRNGs, are random numbers generated by unpredictable physical methods.

Pseudo-random number generators: English: pseudo-random number generators, abbreviated as: PRNGs, are generated by computers using certain algorithms.

Compare the pictures of the random numbers generated by the two methods.

Random bitmap generated by Random.org (which uses atmospheric noise to generate random numbers, which is generated by thunderstorms in the air):

Random pictures generated by the rand() function of PHP under Windows:

Obviously, the pictures generated by the latter pseudo-random number generator have such obvious stripes.

The code that uses PHP's rand random function to generate this picture is:

The code is as follows:

//需要开启gd库
header("Content-type: image/png");
$im = imagecreatetruecolor(512, 512)
or die("Cannot Initialize new GD image stream");
$white = imagecolorallocate($im, 255, 255, 255);
for ($y=0; $y<512; $y++) {
for ($x=0; $x<512; $x++) {
if (rand(0,1) === 1) {
imagesetpixel($im, $x, $y, $white);
}
}
}
imagepng($im);
imagedestroy($im);

In fact, not all pseudo-random numbers occur The effect of PRNGs is so poor, but the rand() function of PHP under Windows happens to be like this. If the same code is tested under Linux, the resulting picture will not show obvious stripes. Under Windows, if the mt_rand() function is used instead of the rand() function, the effect will be much better. This is because mt_rand() uses the Mersenne Twister algorithm to generate random numbers. The PHP documentation also says: mt_rand() can generate random values ​​​​on average four times faster than the rand() provided by libc.

The following is an example code that uses PHP to generate better pseudo-random numbers than using the mt_rand() function:

The code is as follows:

<?php
// get 128 pseudorandom bits in a string of 16 bytes
$pr_bits = &#39;&#39;;
// Unix/Linux platform?
$fp = @fopen(&#39;/dev/urandom&#39;,&#39;rb&#39;);
if ($fp !== FALSE) {
$pr_bits .= @fread($fp,16);
@fclose($fp);
}
// MS-Windows platform?
if (@class_exists(&#39;COM&#39;)) {
try {
$CAPI_Util = new COM(&#39;CAPICOM.Utilities.1&#39;);
$pr_bits .= $CAPI_Util->GetRandom(16,0);
// if we ask for binary data PHP munges it, so we
// request base64 return value. We squeeze out the
// redundancy and useless ==CRLF by hashing...
if ($pr_bits) { $pr_bits = md5($pr_bits,TRUE); }
} catch (Exception $ex) {
// echo &#39;Exception: &#39; . $ex->getMessage();
}
}
if (strlen($pr_bits) < 16) {
// do something to warn system owner that
// pseudorandom generator is missing
}
?>

So PHP To generate truly random numbers, you still need to call external elements to support it!

The above is the detailed content of Detailed explanation of pseudo-random numbers and true random numbers in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn