Table of Contents
Detailed explanation of pseudo-random numbers and true random numbers in PHP, detailed explanation of pseudo-random numbers
Home Backend Development PHP Tutorial Detailed explanation of pseudo-random numbers and true random numbers in PHP, detailed explanation of pseudo-random numbers_PHP tutorial

Detailed explanation of pseudo-random numbers and true random numbers in PHP, detailed explanation of pseudo-random numbers_PHP tutorial

Jul 13, 2016 am 09:52 AM
php pseudorandom number random number

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

The first thing that needs to be stated is 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, which means that the pseudo-random numbers generated by computers 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 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 PHP’s rand() function under Windows:

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

The code to use PHP’s rand random function to generate this picture is:
Copy code The code is as follows:
//Need to enable gd library
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 number generators (PRNGs) are so bad, it just happens that the rand() function of PHP under Windows is 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.

In addition, the Linux kernel (1.3.30 and above) includes a random number generator /dev/random, which is sufficient for many security purposes.

The following is an introduction to the principle of Linux random number generator:

The Linux operating system provides library data that is inherently random (or at least has components with strong randomness). This data usually comes from the device driver. For example, a keyboard driver collects information about the time between two key presses and then fills this ambient noise into a random number generator library.

Random data is stored in an entropy pool (the Linux kernel maintains an entropy pool to collect environmental noise from device drivers and other sources. Theoretically, the data in the entropy pool is completely random and can generate true random numbers Sequence. To track the randomness of the data in the entropy pool, the kernel estimates the randomness of the data when adding it to the pool. This process is called entropy estimation. The entropy estimate describes the number of random digits contained in the pool, and the larger the value. Indicates the more random the data in the pool. ), which "stirs" each time new data comes in. This stirring is actually a mathematical transformation that helps improve randomness. As data is added to the entropy pool, the system estimates how many truly random bits it has obtained.

Measuring the total amount of randomness is important. The problem is that some quantities are often less random than they appear when first considered. For example, adding a 32-bit number representing the number of seconds since the last keystroke was not actually providing a new 32-bit random information, since most keystrokes are close together.

Once the bytes are read from /dev/random, the entropy pool performs a cryptographic hash using the MD5 algorithm, and the individual bytes in the hash are converted into numbers and returned.

If there are no randomness bits available in the entropy pool, /dev/random waits until there is enough randomness in the pool, without returning a result. This means that if you use /dev/random to generate many random numbers, you will find that it is too slow to be practical. We often see /dev/random generate dozens of bytes of data and then produce no results for many seconds.

Fortunately there is another interface to the entropy pool that can circumvent this limitation: /dev/urandom. This alternative device always returns random numbers even if no randomness is available in the entropy pool. If you take out many numbers without giving the entropy pool enough time to refill, you no longer get the benefit of the combined entropy from all sources; but you can still get very good random numbers from the MD5 hash of the entropy pool! The problem with this approach is that if anyone cracks the MD5 algorithm and learns something about the hashed input by looking at the output, your numbers immediately become completely predictable. Most experts agree that such an analysis is computationally unfeasible. However, /dev/urandom is still considered "less secure" (and generally questionable) than /dev/random.

There is no /dev/random available under Windows, but you can use the CAPICOM.Utilities object provided by Microsoft's "capicom.dll".

The following is an example code that uses PHP to generate better pseudo-random numbers than the mt_rand() function:
Copy code The code is as follows:
// get 128 pseudorandom bits in a string of 16 bytes

$pr_bits = '';

// Unix/Linux platform?
$fp = @fopen('/dev/urandom','rb');
if ($fp !== FALSE) {
$pr_bits .= @fread($fp,16);
@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 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 'Exception: ' . $ex->getMessage();
}
}

if (strlen($pr_bits) < 16) {
// do something to warn system owner that
// pseudorandom generator is missing
}
?>

So if PHP wants to generate truly random numbers, it still needs to call external elements to support it!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1008017.htmlTechArticleDetailed explanation of pseudo-random numbers and true random numbers in PHP. Detailed explanation of pseudo-random numbers. The first thing to declare is that computers cannot To generate absolutely random random numbers, computers can only generate "pseudo-random numbers". Actually...
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles