Random Numbers

How Can We Generate Random Numbers in Code?

Many programs use a pseudo-random generator with a seed, while secure values come from the operating system. Here is how that choice works, with a C++ example.

On this page
  1. The short answer
  2. Pseudo-random and true random
  3. Step 1: choose a source
  4. Step 2: seed it
  5. Step 3: turn the output into the range you need
  6. When a generator is not enough
  7. Summary

The short answer

Ordinary code cannot make truly random numbers from its own logic alone. Many programs use a pseudo-random generator, which computes a sequence from a starting value called a seed. For security-sensitive values, they read random bytes from the operating system, which gathers unpredictable data from the hardware and the environment. Pseudo-random numbers are fine for games, simulations and shuffles.

Pseudo-random and true random

A pseudo-random generator is a formula. Given the same seed, it returns the same numbers every time. That makes results repeatable, which is useful for testing and debugging. A truly random source depends on physical unpredictability, such as electrical noise or the timing of hardware events, so its output cannot be reproduced by running the same steps again. Hardware random number generators use processes like these.

Step 1: choose a source

For everyday needs, use a pseudo-random engine. In C++ that means std::mt19937 with a distribution from the <random> header. For security-sensitive values, use the cryptographic random function from your operating system or a vetted library instead. The C++ standard does not guarantee that std::random_device is cryptographic, so check your compiler’s documentation before you rely on it for secrets.

Step 2: seed it

A seed sets where the generator starts. There are three common choices:

  • A fixed value, for repeatable results in tests.
  • A value from std::random_device, which asks the system for a value and is meant to be non-deterministic.
  • The current time, which is easy to use but predictable, so avoid it for anything secret.

Step 3: turn the output into the range you need

Raw output is a large integer. A distribution maps it to what you want: whole numbers in a range, decimals between 0 and 1, or one item from a list of values. Here is a small example that rolls a die:

#include <iostream>
#include <random>

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

    std::cout << "Roll: " << dice(gen) << '\n';
}

When a generator is not enough

Pseudo-random numbers suit many programs, but they are a poor fit when a guessable value could cause harm: passwords, encryption keys, session tokens and one-time codes. In those cases, use the cryptographic random function from your operating system or a security library.

Is rand() actually random covers why the older rand() function falls short, and how to generate random numbers in c covers the code.

Summary

  • Pseudo-random generators are fast and repeatable, which suits games and simulations.
  • Operating system sources are unpredictable, which suits secrets.
  • Pick a source, seed it, then map its output to the range you need.

The complete guide to random numbers

How to Generate Random Numbers in C++
Read the guide

Keep exploring

Guides from other topics

All topics →