C++ Random Number Generator

Learn how to generate random numbers in C++ with interactive code examples. Explore rand(), srand(), <random> library, std::uniform_int_distribution, and std::mt19937 with detailed explanations.

C++ Code Editor

C++ Code
#include <iostream>
#include <random>

int main() {
    int min = 1;
    int max = 100;
    int quantity = 5;

    // Modern C++ random number generation
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dis(min, max);

    std::cout << "Modern C++ random numbers:" << std::endl;
    for (int i = 0; i < quantity; i++) {
        int num = dis(gen);
        std::cout << num << " ";
    }
    std::cout << std::endl;

    return 0;
}
Output

This is a browser preview, not execution of Java or C++. Preview values are not suitable for cryptographic use.

Preview example

Classic C++ with rand()

Use the traditional rand() function from <cstdlib> to generate random integers. Simple and widely supported, though not suitable for cryptographic applications due to poor randomness quality.

Need a Quick Random Number?

Try our online random number generator for instant results without writing any code.

Open Random Number Generator

Understanding C++ Random Number Generation

rand() uses an implementation-specific algorithm. C++11 random facilities separate engines such as std::mt19937 from distributions; a distribution has no default mt19937 engine.

rand() Function

rand() uses an implementation-specific algorithm. C++11 random facilities separate engines such as std::mt19937 from distributions; a distribution has no default mt19937 engine.

<random> Library

The <random> library (C++11+) provides modern random number generation with std::mt19937 (Mersenne Twister), various distributions like uniform_int_distribution, and better randomness quality than rand().

uniform_int_distribution

std::uniform_int_distribution provides precise control over integer ranges with uniform distribution. Perfect for statistical applications, simulations, and when exact range control is needed.

std::mt19937

rand() uses an implementation-specific algorithm. C++11 random facilities separate engines such as std::mt19937 from distributions; a distribution has no default mt19937 engine.

Common Use Cases

Gaming & Simulations

Generate random dice rolls, card draws, procedural generation, and game events using rand() or modern <random> library in game loops and simulations.

Security & Cryptography

rand() and std::mt19937 are not cryptographic generators. For security-sensitive output, use a documented cryptographic library or operating-system API; std::random_device is not a portable security guarantee.

High-Performance Computing

The modern <random> library with std::mt19937 provides excellent performance for generating large quantities of random numbers in HPC and scientific computing applications.

Software Testing

Create randomized test data, fuzz testing inputs, and edge cases to ensure robust code coverage and bug detection in C++ applications.

How C++ Random Number Generation Works

rand() uses an implementation-specific algorithm. C++11 random facilities separate engines such as std::mt19937 from distributions; a distribution has no default mt19937 engine.

rand() uses an implementation-specific algorithm. C++11 random facilities separate engines such as std::mt19937 from distributions; a distribution has no default mt19937 engine.

std::random_device depends on the implementation and may use a deterministic engine. It does not guarantee unpredictable output on every platform.

Practical tips

🎯

Avoid rand() for New Code

rand() uses an implementation-specific algorithm. C++11 random facilities separate engines such as std::mt19937 from distributions; a distribution has no default mt19937 engine.

🔒

Choose an explicit seed

A fixed seed reproduces the same engine sequence. C++ engines are not automatically seeded from system entropy; choose a seed explicitly when that is needed.

⚡

Use Appropriate Distributions

For uniform integer ranges, use std::uniform_int_distribution. For floating-point numbers, use std::uniform_real_distribution. This ensures proper statistical properties.

Technical Specifications

Algorithms

  • rand() uses an implementation-specific algorithm. C++11 random facilities separate engines such as std::mt19937 from distributions; a distribution has no default mt19937 engine.
  • Mersenne Twister (std::mt19937)
  • std::random_device depends on the implementation and may use a deterministic engine. It does not guarantee unpredictable output on every platform.

Performance Characteristics

  • rand(): Fast but poor randomness quality
  • <random>: Fast with excellent quality
  • std::mt19937: Very fast for large quantities

Frequently Asked Questions

Which C++ approaches are covered?

Classic rand() with srand(), the modern <random> library, std::uniform_int_distribution and the mt19937 engine.

Can I tailor the examples?

Set integer bounds and quantity to generate copyable C++ examples. The browser preview uses JavaScript Math.random() to illustrate the range; it does not compile C++ or reproduce a rand() or mt19937 sequence.

Which approach is recommended today?

The <random> library with a named distribution and engine; rand() survives in old code but has weak quality and range pitfalls.

How do I seed the generator?

std::random_device depends on the implementation and may use a deterministic engine. It does not guarantee unpredictable output on every platform.

What range does rand() return in C++?

rand() returns 0 through RAND_MAX inclusive. Check the platform: the Microsoft CRT uses 32767; do not assume the same limit elsewhere.

Is rand() cryptographically secure?

rand() and std::mt19937 are not cryptographic generators. For security-sensitive output, use a documented cryptographic library or operating-system API; std::random_device is not a portable security guarantee.

How do I generate random floats in C++?

Combine std::mt19937 with std::uniform_real_distribution configured for your interval, for example 0.0 to 1.0, then call dist(generator). The result is a double where the lower bound is included but the upper bound is excluded.

Is rand() thread-safe in C++?

The thread safety of rand() depends on the implementation. Give each thread its own engine or synchronize access to a shared engine.

Does the generator remember my previous inputs and settings?

Yes. Everything you type and every option you choose is kept only in your own browser's local storage on this device. Reload the page or come back later and your last text, quantities, and format choices are restored instantly, with no account and no upload — nothing is stored on any server, and a different device or browser keeps its own separate memory. Clearing a field removes it from memory immediately, and emptying every field makes the tool forget everything. Uploaded files are never saved; only plain settings such as sizes, formats, styles, and separators are remembered on your device, and you can change them at any time.

C++ standard draft: rand / srand · C++ standard draft: random_device · C++ standard draft: uniform_int_distribution

References & Resources

Programming tools and code generators for developers. Learn how to generate random numbers, strings, and data in various programming languages