Random numbers are an essential component of many applications, including cryptography, simulations plus gaming. A random number generator is an algorithm that produces a sequence of numbers that are unpredictable and uniformly distributed. In this article, we will explore the basics of random number generation and discuss how to create an algorithm to generate random numbers.
Types of Random Number Generators
There are several types of random number generators, including true random number generators and pseudorandom number generators. True random number generators use physical processes, such as atmospheric noise or radioactive decay, to generate random numbers. Pseudorandom number generators, on the other hand, use mathematical algorithms to generate a sequence of numbers that appear to be random.
Pseudorandom number generators are widely used because they are fast, reliable plus can produce a large number of random numbers; the most common type of pseudorandom number generator is the linear congruential generator (LCG), which uses a simple mathematical formula to generate a sequence of numbers.
Start your morning with the signal that matters.
Weekday mornings. Five minutes. What changed in security, why it matters.
By subscribing you agree to our Privacy Policy.
Free. Weekday mornings. 5 minutes or less.
Linear Congruential Generator (LCG)
The LCG algorithm is based on the following formula:
X_i = (a * x_{i-1} + c) % m
Where x_i is the i-th number in the sequence. X_{i-1} is the previous number in the sequence, an is a constant called the multiplier, c is a constant called the increment. M is a constant called the modulus. The initial value of x, called the seed, determines the starting point of the sequence.
The LCG algorithm can be implemented in several programming languages, including C, C++, and Python. The following code demonstrates how to implement the LCG algorithm in C++:
#include <iostream>
#include <cstdlib>
#include <ctime>
int main() {
srand(time(0));
int x = rand();
for (int i = 0; i < 10; i++) {
x = (1103515245 * x + 12345) % 2147483647;
std::cout << (double) x / 2147483647 << std::endl;
}
return 0;
}
This code generates 10 random numbers in the range [0,1) using the LCG algorithm. To change the seed value, simply modify the value of x before the for loop.
Evaluating Random Numbers
To evaluate the quality of random numbers generated by a random number generator, several statistical tests can be used. The most common tests include the chi-squared test, the Kolmogorov-Smirnov test, and the Runs test. These tests check the randomness of the numbers generated by the generator and whether they are uniformly distributed.
Start your morning with the signal that matters.
Weekday mornings. Five minutes. What changed in security, why it matters.
By subscribing you agree to our Privacy Policy.
Free. Weekday mornings. 5 minutes or less.
The chi-squared test determines if the distribution of generated numbers is uniform. The test divides the range of generated numbers into k intervals and counts the number of generated numbers in each interval. If the generator is producing numbers that are uniformly distributed, the number of generated numbers in each interval should be approximately the same. The test calculates a chi-squared statistic based on the observed and expected number of generated numbers in each interval and compares it to a critical value. If the calculated statistic is larger than the critical value, it indicates that the generator is not producing numbers that are uniformly distributed.
The Kolmogorov-Smirnov test determines if the distribution of generated numbers is different from a uniform distribution. The test calculates the largest difference between the cumulative distribution function (CDF) of the generated numbers and the CDF of the uniform distribution. If the difference is large, it indicates that the generator is not producing numbers that are uniformly distributed.
The Runs test determines if the generated numbers are random by counting the number of runs in the sequence. A run is a sequence of consecutive numbers that have the same sign. If the generator is producing numbers that are random, the number of runs should be close to the expected number of runs for a random sequence.
Where things stand
Generating random numbers is an important task in many applications. Pseudorandom number generators, such as the LCG, are widely used because of their fast speed, reliability plus ability to produce a large number of random numbers. When evaluating the quality of generated numbers, statistical tests such as the chi-squared test, the Kolmogorov-Smirnov test, and the Runs test can be used. By understanding the basics of random number generation and following the steps outlined in this article, developers can create their own algorithms to generate random numbers.
Newer CyberExperts coverage on this topic
This article still works as background. If you want the current picture, start with the freshest related coverage below and today's brief.
Breaking Cyber and Physical Security Silos to Enhance Corporate Security
Corporate security spans digital systems and physical spaces, but the teams responsible for each often work apart. Breaking down those silos —...
How to Lower Your Cyber Insurance Premiums in 2027
A costly cyber insurance renewal doesn't have to mean cutting coverage. These strategies give insurers clear evidence of lower risk, from ISO...
Network Security Certifications (2026): Network+, CCNA, CCNP Security, Fortinet NSE, Palo Alto and GCIA
Network security certifications compared: Network+, CCNA, CCNP Security, Fortinet NSE 4 (formerly FCP), the Palo Alto Networks certs that replaced PCNSA and...
Thursday’s brief: Java4Shell first, then nginx, F5 BIG-IP and Synology
The fastest way to catch up on what changed after this article was published.
Start your morning with the signal that matters.
Get the biggest cybersecurity developments, why they matter, and where to go deeper on CyberExperts.
By subscribing you agree to our Privacy Policy.
Free. Weekdays. Built for operators.