# random numbers

### What is a random number?

As the term suggests, a random number is a number chosen by chance -- i.e., randomly, from a set of numbers. All the numbers in a specified distribution have equal probability of being chosen randomly.

A random number occurs in a specified distribution only when two conditions are met: The values are uniformly distributed over a defined interval or set, and it is impossible to predict future values based on past or present ones.

Random numbers are important in statistical analysis, probability theory, and modern-day computer simulations, digital cryptography and cryptocurrency wallets.

### Random numbers explained

For a number in a sequence or distribution to be truly random, it must be independent. The independence of numbers means there is no correlation between successive numbers. In addition, these numbers should occur in the distribution with approximately the same frequency.

Random numbers are almost always derived from a set of single-digit decimal numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. The task of generating random digits from that set of numbers by physical means is not trivial. A common scheme is the selection of numbered pingpong balls from a set of 10, as frequently seen in lotto games and lotteries.

Each ball bears a digit and is circulated in a container by an air jet. After each number is selected, the ball with that number is returned to the set. The balls are again blown around before another ball is selected. The process continues until the desired set of random numbers has been generated.

In general, it is extremely difficult for humans to produce an arbitrary long string of digits and prove that it is fully random, so computer programs and algorithms are required. These programs can predict some digits in a distribution based on previous digits to produce a fairly random distribution.

### Generating random numbers from irrational numbers

Sometimes, the digits in the decimal expansions of irrational numbers are used to obtain random numbers. Most whole numbers have irrational square roots, so entering a string of six or eight digits into a calculator and then hitting the square root button can provide a sequence of digits that seems random. Other algorithms have also been devised that supposedly generate random numbers.

The problem with those methods is that they violate the second condition in the definition of randomness as noted above. The existence of any number-generation algorithm produces future values based on past and/or current ones. Digits or numbers generated in this manner are called *pseudorandom*.

### Methods of producing random numbers

Statisticians, mathematicians and scientists have long searched for the ideal source of random numbers. One frequently used method was the sampling of electromagnetic noise. This noise -- generated by the chaotic movements of electrons, holes or other charge carriers in materials and in space -- is thought to be as close to "totally random" as any observable phenomenon. Another method that also generates random numbers is radioactive decay*. *Numbers generated as the output of such unpredictable physical processes are deemed random or "random with a uniform distribution."

Today, random numbers can be generated with the help of hardware as well as software. Computer-generated random numbers are sometimes referred to as pseudorandom numbers to differentiate them from the "truly" random numbers generated from physical processes and hardware.

The numbers are also deemed pseudorandom because some arithmetic procedure is used to generate them, and even though the generated random number sequence may form a pattern, the numbers in them may not be truly random.

The linear congruence method is a popular arithmetic method to generate pseudorandom numbers.

### Linear congruence method

The linear congruence method, also known as the linear congruential method, of producing random numbers is a type of pseudorandom number generator (PRNG) algorithm to generate a sequence of random numbers within a specific number range or distribution.

In this algorithm, the equation is defined as *X _{i+1} = aX_{i} + c mod m* where

*X*is the sequence of pseudorandom numbers;

*m*is the modulus parameter, m > 0;

*a (0, m)*is the multiplier;

*c (0, m)*is the increment;

*X*is the initial ("seed") value of the sequence; and

_{0 }(0, m)*m*,

*a*,

*c*and

*X*

_{0}_{ }should be chosen appropriately to get a period almost equal to

*m*.

The first step to generate a random number sequence is to choose *X _{0}*,

*m*,

*a*and

*c*. The algorithm will then initialize the required number of random numbers to be generated, as well as defining the storage to store the generated random number sequence.

The 0 index of the vector will be initialized with the seed value. The linear congruential method will be applied for the remaining indexes to generate the random numbers per the *randomNums[i] = ((randomNums[i – 1] * a) + c) % m* logic.

A number of languages use this logic to generate random numbers. The output of this logic in C++, Java, Python, C# and JavaScript will be: 5 4 1 6 0 3 5 4 1 6.

### Random number generator

In modern-day applications, random numbers are commonly produced with a random number generator (RNG), which can be either a hardware device or software algorithm. The device or algorithm randomly generates a number that's taken from a limited or unlimited distribution.

There are generally two types of random number generators: pseudorandom number generators and true random number generators.

**Pseudorandom number generator. **Software-based PRNGs use algorithms to mimic the selection of a value and approximate true randomness. However, their outputs are not truly random numbers because the computer uses an algorithm based on a distribution, hence the term *pseudorandom*. Further, the quality of randomness produced with PRNGs varies.

PRNGs rely on deterministic, predictable algorithms. Also, a seed number can be set to replicate the random numbers generated. So, if the seed is known, the numbers can also be predicted. This is also why PRNGs do not produce truly random numbers and are not as secure as true random number generators.

PRNGs are used in computer languages like Python, applications such as Excel and in stream ciphers like ChaCha. However, they are not suitable for secure cryptographic implementations. For such uses, a cryptographically secure PRNG is required. A combination of algorithms can also help achieve a more secure approximation of a true random number.

**True random number generators. **A true random number generator (TRNG), also known as a *hardware random number generator* (HRNG), does not use a computer algorithm. Instead, it uses an external unpredictable physical variable such as radioactive decay of isotopes or airwave static to generate random numbers. Similarly, subatomic particles are also ideal variables of an unpredictable system since they exhibit truly and completely random behavior, also known as *pure randomness*.

Because a TRNG is based on physical processes, it considers physical attributes of the system such as atmospheric or thermal conditions. It may also take into account measurement biases. TRNGs and HRNGs are cryptographically secure and are useful for creating seed tokens.

Most RNGs start with an initial number known as a *seed*. A random seed or seed state is simply a number or vector used to initialize an RNG (usually a PRNG). An examination of the algorithm's noise sphere can reveal the "goodness" of the random numbers generated by it.

### Challenges and risks of generating random numbers

Despite the availability of physical processes and computer algorithms, producing truly random numbers is still a challenging endeavor. In addition, there is always a risk that the random numbers produced may be compromised or exploited. Such compromise may result in the loss of confidentiality in file systems, source code, memory or network communications.

If a leak happens on a computer that's connected to other devices, the leak can increase the risk of compromise of all the connected devices. That's why computers that produce random numbers for specific use cases are more secure than general-purpose computers. Examples of such use cases are mnemonic phrase generation and the storage of cryptocurrency phrases and keys.

Another challenge is that true RNGs are rarely cost-efficient. They are also subject to gradual decline and often rely on deterministic post-processing algorithms to further improve randomness. That said, using multiple random processes can make the random number generation process more effective. This can be done by combining TRNG and PRNG or by using an ensemble of algorithms in a cipher suite.

### Applications of random numbers and random number generators

In general, it is difficult to program a computer to generate random numbers since computers usually produce only predictable inputs based on what they are programmed to do. RNGs enable computers to generate unique, nonuniform and random numbers. They are used in numerous real-world applications, including the following:

**Cryptography****.**Random numbers are crucial for the digital encryption of passwords, browsers and other online and digital data.**Cryptocurrency wallets.**Seed phrases calculated with random numbers are used for BIP39 standard algorithms, which are used to calculate encryption keys for cryptocurrency wallets.-
**Simulations.**Pseudorandom number sequences are used to test and re-run simulations, such as Monte Carlo simulations, to sample something for the estimation of unknown ratios and areas. **Machine learning (ML).**Random numbers and ML model-free learning frameworks such as domain randomization (DR) are used in many real-world applications, including robotic vacuum cleaners and the OpenAI hand dexterity learning project.- Random numbers and PRNGs are used in many aspects of game development, including gameplay and graphics.
**Computing****.**Random numbers are important for TCP/IP sequence numbers, Transport Layer Security nonces, password salts and DNS source port numbers.**Scientific studies.**Random numbers are used to construct probability samples from a population and make statistical inferences from a survey, and also to decide which treatment should be applied to the various physical units in an experiment.-
**Mathematics.**RNGs protect against selective bias, which makes them useful for solving mathematical problems that appear completely non-probabilistic as well as statistical problems that are hard to solve by direct analysis.

*See also: hardware security module**, cryptosystem**, Luhn algorithm**.*