Back to C++
2026-04-037 min read

Random Number Generator (C++)

Learn Random Number Generator (C++) step by step with clear examples and exercises.

Title: Random Number Generator (C++)

Why This Matters

In programming, generating random numbers is a fundamental task that can be used for various purposes such as simulations, cryptography, game development, and more. In this lesson, we will delve deeper into the C++ library functions to generate random numbers, understand their properties, and learn how to use them effectively.

The Importance of Random Number Generation in Programming

Random number generation is essential for creating realistic simulations, generating test cases, implementing cryptographic algorithms, and designing games. By understanding how to generate random numbers in C++, you will be able to create more robust and versatile programs that can tackle a wide range of problems.

Prerequisites

Before diving into generating random numbers in C++, you should have a solid understanding of the following concepts:

  1. Basic C++ syntax (variables, operators, control structures)
  2. Standard Template Library (STL) – especially the ` header and other essential headers like , , and `
  3. I/O Streams (std::cout, std::cin)
  4. Functions and function overloading
  5. Understanding of data structures such as arrays, vectors, and strings
  6. Familiarity with conditional statements, loops, and exception handling
  7. Knowledge of object-oriented programming concepts like classes and inheritance (optional but helpful)

Core Concept

The C++ standard library provides a powerful tool for generating random numbers through the `` header. This header includes several classes and functions to generate various types of random numbers, including uniform distributions, normal distributions, and more.

Random Device

The primary class used for generating random numbers is std::random_device. It provides a source of unpredictable numbers that can be used as seeds for other random number generators. However, Note that that std::random_device may not always provide the best performance and may not be suitable for high-performance applications.

Random Number Generators (RNG)

The C++ standard library offers several types of random number generators, each with its own properties and characteristics. The most commonly used ones are:

  1. std::mt19937 – a Mersenne Twister pseudorandom number generator with 19937 bits of state (a popular choice due to its high quality and speed)
  2. std::minstd_rand0 – another pseudorandom number generator, less powerful but faster than std::mt19937
  3. std::ranlux24_base – a random number generator based on the Mersenne Twister with additional features for better performance and portability
  4. std::discard_block – a class that discards a specified number of random numbers from a given engine to improve its quality

Distributions

Distribution classes in C++ are used to generate specific types of random numbers according to a certain probability distribution. The most commonly used ones are:

  1. std::uniform_int_distribution – generates uniformly distributed integers within a specified range
  2. std::uniform_real_distribution – generates uniformly distributed real numbers within a specified interval
  3. std::normal_distribution – generates normally (Gaussian) distributed random numbers with user-defined mean and standard deviation
  4. std::exponential_distribution – generates exponential distributed random numbers, useful for modeling waiting times in various systems
  5. std::poisson_distribution – generates Poisson distributed random numbers, representing the number of events occurring within a fixed time interval with a given rate

Generating Random Numbers

To generate random numbers using the above classes, follow these steps:

  1. Include the necessary headers:
#include <random>
#include <iostream>
#include <vector>
#include <string>
  1. Create an instance of a random number generator (e.g., std::mt19937). You can seed it with a value from std::random_device.
std::random_device rd;
std::mt19937 gen(rd());
  1. Create an instance of the distribution class (e.g., std::uniform_int_distribution). Specify the minimum and maximum values for the range you want to generate random numbers within.
std::uniform_int_distribution<int> dis(1, 100); // Generate integers between 1 and 100
  1. Use the generator and distribution objects together to produce a random number:
int num = dis(gen);
std::cout << "Random Number: " << num << std::endl;

Worked Example

Let's create a simple program that generates 10 random numbers between 1 and 100 using std::mt19937 and std::uniform_int_distribution. We will also use a vector to store the generated numbers.

#include <random>
#include <iostream>
#include <vector>
#include <string>

int main() {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int> dis(1, 100);

std::vector<int> numbers(10);
for (auto &num : numbers) {
num = dis(gen);
std::cout << "Random Number: " << num << std::endl;
}

return 0;
}

Common Mistakes

  1. Forgetting to seed the random number generator (e.g., std::mt19937) with a value from std::random_device.
  2. Using an incorrect or outdated random number generator (e.g., using std::rand() instead of std::mt19937).
  3. Not specifying the correct range for the distribution class (e.g., providing invalid minimum and maximum values).
  4. Failing to include the necessary headers (`, `) or forgetting to link the C++ Standard Library during compilation.
  5. Misunderstanding the difference between pseudorandom number generators (PRNG) and true random number generators (TRNG). PRNGs use deterministic algorithms to produce sequences of numbers that appear random, while TRNGs rely on unpredictable sources such as hardware noise or user input.
  6. Not considering the performance implications when choosing a random number generator for specific applications.
  7. Failing to discard initial values from a PRNG to improve its quality (using std::discard_block).
  8. Not properly handling exceptions that may occur during the generation of random numbers.
  9. Forgetting to initialize variables before using them in the program.
  10. Using incorrect syntax or forgetting to include necessary libraries when working with STL classes and functions.

Practice Questions

  1. Write a program that generates 20 random numbers between 1 and 10 using std::uniform_int_distribution.
  2. Modify the previous example to generate real numbers (floating-point) uniformly distributed between 0 and 1.
  3. Create a program that simulates rolling a six-sided die 1,000 times and calculates the average roll. Use std::uniform_int_distribution and std::mt19937.
  4. Write a program that generates a random password consisting of uppercase letters, lowercase letters, digits, and special characters. Use std::string, std::random_shuffle, and the necessary distribution classes.
  5. Implement a simple guessing game where the computer randomly selects an integer between 1 and 100, and the user tries to guess it. Provide feedback on whether the user’s guess is too high or too low. Use std::uniform_int_distribution and std::mt19937.
  6. Write a program that generates a random permutation of an array using std::random_shuffle from the STL.
  7. Implement a program that generates normally distributed random numbers with a mean of 50 and standard deviation of 10, and calculates the probability of generating a number greater than 60.
  8. Write a program that simulates the Monty Hall problem using std::uniform_int_distribution and std::mt19937.

FAQ

  1. Why should I use C++ for generating random numbers instead of other programming languages like Python?
  • C++ provides lower-level control over memory and performance, making it suitable for applications that require high-speed number generation.
  • C++ offers a more extensive set of random number generators compared to some other languages.
  1. What is the difference between std::random_device and std::mt19937?
  • std::random_device provides an unpredictable seed for other random number generators, while std::mt19937 is a pseudorandom number generator that generates sequences of numbers using a deterministic algorithm.
  1. Can I generate normal (Gaussian) distributed random numbers in C++?
  • Yes, you can use the std::normal_distribution class from the `` header to generate normally distributed random numbers with user-defined mean and standard deviation.
  1. What is the best way to seed std::random_device?
  • Seeding std::random_device with a non-deterministic source (e.g., system clock, hardware noise) can help ensure unpredictable results. However, in some cases, using a fixed seed value may be necessary for reproducible test results.
  1. Why is it important to use a good quality random number generator like Mersenne Twister?
  • A high-quality random number generator helps ensure that the generated numbers are truly unpredictable and uniformly distributed, which is crucial in applications where randomness plays an essential role (e.g., simulations, cryptography).
  1. Why does C++ not have a built-in function for generating random numbers like Python's randint()?
  • C++ relies on the Standard Template Library (STL) to provide a more flexible and extensible solution for generating random numbers. The STL offers various classes and functions that can be used to generate different types of random numbers, making it a powerful tool for developers.
  1. Can I use other libraries or third-party solutions for generating random numbers in C++?
  • Yes, there are several libraries available for generating random numbers in C++, such as Boost.Random and GSL (GNU Scientific Library). These libraries offer additional features and distribution classes that may not be available in the standard library.
  1. How can I ensure that my random number generator is truly unpredictable?
  • To ensure the unpredictability of your random number generator, you should seed it with a non-deterministic source (e.g., system clock) and use a high-quality pseudorandom number generator like Mersenne Twister. Additionally, you can test your generator using statistical tests to verify its properties.
Random Number Generator (C++) | C++ | XQA Learn