Very commonly, computer systems require random values for different processes, for example, to sort things, to select elements from a set, for simulations, to take different paths in a process (such as video games).
One area where random values have special importance is in phone number library cryptography and computer security. In fact, random numbers are closely related to the generation of values usable in cryptography, such as keys, for example.
Let’s look at how random numbers are generated and how you can use them correctly, but first, let’s start by talking about what randomness is and how we can intuitively understand it.
RandomnessPermalink
Randomness refers to the probability of obtaining a certain value from a set (universe) of possible values. Random values are unpredictable; you can’t guarantee that one is more likely to appear than another. Now connect the two previous concepts: for a value to be random, it must be drawn from a set of values with the same probability of appearing, which is known as a uniform distribution .
Example: A coin toss can yield two values: heads or tails (tails or tails in Mexico). It’s impossible to guarantee that one or the other value will come up, because both values have the same probability of coming up. And many physical processes behave in this same way.
If a certain value is more likely to appear in the universe of possible values, then we begin to lose randomness; imagine, for example, the case of a loaded coin or die.
Humans perceive a value as random if it seems unlikely that we obtained that value specifically, but this is not always the case.
Randomness can be measured by entropy , which is the amount of information available in the entire value space. Entropy is the sum of the probability of each value appearing multiplied by its logarithm base 2, and is measured in bits . A perfectly random generator delivers as many bits of entropy as possible values .
Random number generatorsPermalink
We programmers can use randomness if we have a random number generator .
A random number generator is a program that gives you a series of random bits—that is, bits that are unpredictable from an external perspective. These bits can then be used to create a random number.
There are different types of generators depending on their source of entropy (unpredictable information) and how they use it. Let’s talk about the different types, their characteristics, and how to use them.
True Random Number GeneratorsPermalink
This class of generators is also known simply as random number generators ( RNGs ). RNGs derive their entropy from physical locations , since the real world is unpredictable. They measure variations in semiconductors, the way you move your mouse or keyboard, information from computer sensors, your microphone, the network, and many other things. Some even derive their entropy from quantum processes, known as quantum random number generators or QRNGs .
These physical elements are reliable sources of entropy, but we can’t trust our way of measuring or “capturing” it, and they can be skewed by a malicious user or attacker. They are also slow to generate the random bits that applications may require.