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
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.
#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.
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?
Yes. Set the minimum, maximum and quantity in the parameters panel, then run to see the output.
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.
C++ standard draft: rand / srand · C++ standard draft: random_device · C++ standard draft: uniform_int_distribution
References & Resources
-
C++ <random> Reference - cppreference
The official C++ pseudo-random number generation library reference.
-
Mersenne Twister - Wikipedia
The Mersenne Twister algorithm behind most language random libraries.