HOWTO · C++

How to Generate a Random Number Between 0 and 1 in C++

Generate a floating-point value from 0 inclusive to 1 exclusive in C++ with <random>, and understand the legacy std::rand option.

On this page

To generate a floating-point pseudo-random value from 0 up to (but not including) 1, pair a C++11 random engine with std::uniform_real_distribution<double>. The example uses a fixed seed, so the same engine and seed reproduce a sequence on a given implementation. Use <random> for new C++ code; keep std::rand for maintaining older code.

Use C++11 <random> to generate a floating-point number

Include <random>, create an engine, and pass it to a distribution whenever you need a value. An engine generates a sequence of pseudo-random bits; the distribution maps those bits to values of the requested type and range. This complete example prints five values. Its fixed seed makes reruns reproducible on the same standard library implementation.

#include <iostream>
#include <random>

int main() {
    std::mt19937 engine(2026);
    std::uniform_real_distribution<double> distribution(0.0, 1.0);

    for (int i = 0; i < 5; ++i) {
        std::cout << distribution(engine) << '\n';
    }
}

The printed digits depend on the standard library implementation. On a given implementation, using the same engine and seed starts the same sequence. To choose another sequence, use a different seed or initialize the engine once from std::random_device; that device may not provide nondeterministic entropy on every system.

Understand the output range and choose an engine

For std::uniform_real_distribution<double>(0.0, 1.0), the specified interval is half-open: 0 can be returned, while 1 is excluded. The template argument selects the floating-point result type (float, double, or long double). A distribution needs an engine argument; it is not a standalone generator.

The engine determines the pseudo-random sequence, and the seed determines where that sequence starts. std::mt19937 is a widely used standard engine with a specified algorithm. std::default_random_engine is convenient when the particular engine is unimportant, but its choice is implementation-defined, so it is less suitable when you need the same sequence across different standard libraries. Seed an engine once and reuse it; reseeding before every draw can repeat values and harms the sequence. These facilities suit simulation, games, and ordinary randomized behavior, but not cryptographic secrets.

Use std::rand only for legacy code

Older code may use std::rand, declared in <cstdlib>. It returns an integer from 0 through RAND_MAX, inclusive. Convert to floating point before dividing to avoid integer division. For example, the inline expression static_cast<double>(std::rand()) / (static_cast<double>(RAND_MAX) + 1.0) scales the integer into the desired range.

This scaling keeps the result below 1, including when std::rand returns RAND_MAX. To vary runs, call std::srand once before generating values, often with a time-based seed; repeated runs can still use the same seed, and seeding does not improve the generator’s quality. RAND_MAX is implementation-dependent, and std::rand offers weaker control over the engine and sequence than <random>. Prefer the C++11 engine and distribution for new code, and use the legacy function only where an existing interface requires it.