C++ random number generator code with integer and floating-point distributions

How to Generate a Random Number in C++

To generate a random number in C++, use the modern <random> library instead of the older rand() function. You combine an engine that produces a sequence, a seed that initializes that sequence, and a distribution that defines the values you want.

The same design supports random integers, decimal values, repeatable tests, and nondeterministic application behavior. The examples below use std::mt19937, a widely used C++ random number generator engine.

Generate a random number in C++ with <random>

Include <random>, create an engine, and pass that engine to a distribution. The distribution determines the type and range of the result; the engine does not determine whether you get an integer or a floating-point value.

Seed the engine once before drawing values. Reusing the same engine advances its internal state, so each call normally produces the next value in its sequence.

Engines, seeds, and distributions for a C++ random number generator

  • Engine: std::mt19937 generates a deterministic sequence of pseudorandom bits.
  • Seed: An initial value selects where the engine starts in its sequence.
  • Distribution: A type such as std::uniform_int_distribution converts engine output into a requested range.

A fixed seed makes results repeatable, which is useful for unit tests, debugging, simulations, and examples. Avoid reseeding before every draw; doing so can repeat values or reduce the quality of the sequence.

Generate a random integer in a range

Use std::uniform_int_distribution<int> when you need whole numbers. Both bounds are inclusive, so this distribution can return any integer from 1 through 100, including 1 and 100.

Complete integer example: #include <iostream> #include <random> int main() { std::mt19937 engine(12345); std::uniform_int_distribution<int> distribution(1, 100); int value = distribution(engine); std::cout << value << ‘\n’; }

Change the constructor arguments to select another inclusive range, such as (-10, 10). The distribution handles the mapping correctly, unlike manually applying modulo arithmetic to rand(), which can produce uneven results and makes range behavior less clear.

Generate a random floating-point value

Use std::uniform_real_distribution<double> for decimal values. Its lower bound is inclusive and its upper bound is exclusive in the usual interval form [a, b). Therefore, (0.0, 1.0) produces values from 0.0 up to, but generally not including, 1.0.

Complete floating-point example: #include <iostream> #include <random> int main() { std::mt19937 engine(12345); std::uniform_real_distribution<double> distribution(0.0, 1.0); double value = distribution(engine); std::cout << value << ‘\n’; }

For a different decimal range, pass new lower and upper bounds, such as (10.0, 20.0). To request a different sequence each run, seed the engine from std::random_device instead of a fixed value:

std::random_device seed_source; std::mt19937 engine(seed_source());

A fixed seed is predictable and repeatable; random_device requests a nondeterministic seed source where the platform supports one.