A detailed example of pseudo-random numbers and true random numbers in PHP

Source: Internet
Author: User
This article mainly introduces the pseudo-random number and the true random number of PHP, this article first explains the true random number and pseudo-random number of the relevant concepts, and gives a better pseudo-random number than the Mt_rand () function to produce a sample code, the need for friends can refer to the next

The first thing to declare is that the computer does not produce an absolute random number, and the computer can only produce "pseudo-random numbers". In fact, an absolute random random number is only an ideal random number, and even if the computer develops, it will not produce a random number of absolute random numbers. A computer can only generate a relative random number, or pseudo-random number.

Pseudo-random number is not a pseudo-random number, here the "pseudo" is a regular meaning, is the computer generated pseudo-random number is both random and regular. How to understand it? The generated pseudo-random numbers sometimes obey certain laws, sometimes do not obey any laws, some of the pseudo-random numbers obey certain laws, and the other part does not obey any laws. For example, "There are no two identical leaves in the world", this is the point to the nature of things, that is, randomness, but each tree leaves have an approximate shape, which is the commonality of things, that is, regularity. From this point of view, you will probably accept the fact that the computer can only produce pseudo-random numbers and not produce absolutely random random numbers.

First, let's look at the concepts of true random numbers and pseudo-random numbers.

True random number generator: in English: True random number generators, referred to as: Trngs, is the use of unpredictable physical means to generate random numbers.

Pseudo-random number generator: English: pseudo-random numbers generators, referred to as: PRNGs, is a computer using a certain algorithm to produce.

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

A random bitmap generated by random.org (using atmospheric noise to generate random numbers, while atmospheric noise is produced by thunderstorms in the air):

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

Obviously, the latter pseudo-random number generator produces pictures that have this obvious stripe.

The code that uses the rand random function of PHP to generate this image is:

The code is as follows:

You need to open the GD Library header ("Content-type:image/png"); $im = Imagecreatetruecolor (up to,) 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 (ran D (0,1) = = 1) {Imagesetpixel ($im, $x, $y, $white);}} Imagepng ($im); Imagedestroy ($im);

In fact, not all pseudo-random number generators (prngs) effects are so bad, just the rand () function of PHP that happens to be in Windows. If you test the same code under Linux, the resulting image will not show any noticeable stripes. It would be much better to replace the rand () function with the Mt_rand () function under Windows. This is because Mt_rand () uses the Mersenne Twister (horse's plug rotation) algorithm to generate random numbers. The PHP document also says that the average speed of mt_rand () can produce random values four times times faster than the rand () provided by LIBC.

Here is an example code that uses PHP to produce a better pseudo-random number than the Mt_rand () function:

The code is as follows:

<?php//get pseudorandom bits in a string of bytes$pr_bits = ";//Unix/linux platform? $fp = @fopen ('/dev/urando M ', ' RB '), if ($fp!== FALSE) {$pr _bits. = @fread ($fp, +); @fclose ($FP);} Ms-windows platform?if (@class_exists (' COM ')) {try {$CAPI _util = new COM (' CAPICOM. Utilities.1 '); $PR _bits. = $CAPI _util->getrandom (16,0);//If we ask for binary data PHP munges it, so we//request Base6 4 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 ' Exception: '. $ex->getmessage ();}} if (strlen ($PR _bits) <) {//do something to warn system owner that//pseudorandom Generator is missing}?>

So PHP to generate a true random number or to invoke external elements to support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.