Random Numbers with the random Header
Generate quality random numbers with modern engines and distributions.
Random Numbers with the random Header is a free C++ Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.
Why Not rand()?
The legacy C std::rand() uses a global state, has poor statistical quality, and is not thread safe. Modern C++ provides much better tools in <random>.
The Three-Part Recipe
Random numbers in modern C++ involve three pieces:
- A seed source (true entropy)
- A generator engine (deterministic state)
- A distribution (shapes the output)
Seeding with random_device
std::random_device samples real entropy from the OS — but it can be slow. Use it once to seed a fast engine.
#include <random>
std::random_device rd;
std::mt19937 engine(rd());The mt19937 Engine
std::mt19937 (Mersenne Twister) is a fast pseudo-random engine with excellent statistical quality. The default choice for most applications.
Uniform Integer Distribution
Produces uniformly distributed integers in a closed range.
std::uniform_int_distribution<int> dice(1, 6);
int roll = dice(engine);Uniform Real Distribution
For floating point in a half-open range [a, b).
std::uniform_real_distribution<double> dist(0.0, 1.0);
double x = dist(engine);Normal (Gaussian) Distribution
For bell-curve random numbers — common in simulations and ML.
std::normal_distribution<double> norm(0.0, 1.0); // mean 0, stddev 1
double x = norm(engine);Other Distributions
The library includes many more: bernoulli_distribution, poisson_distribution, exponential_distribution, discrete_distribution, and more.
Reproducible Randomness
For testing, seed with a fixed value to get the same sequence every run.
std::mt19937 engine(42);
// Same output every time you run the programRandom Shuffling
Shuffle a container with std::shuffle, which takes any engine.
#include <algorithm>
std::vector<int> v = {1,2,3,4,5};
std::shuffle(v.begin(), v.end(), engine);Pitfall: Reseeding in a Loop
Do not create a new random_device or engine for each random number. Construct one engine and reuse it across calls.
Quick Check
What is the safest modern C++ way to seed a pseudo-random engine?
Recap
Modern C++ randomness uses random_device for seeding, mt19937 as the engine, and a distribution like uniform_int_distribution to shape the output. Reuse the engine — never create one per call.
Learn C++ with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Random Numbers with the random Header” lesson free?
Yes — any 3 lessons of the C++ Academy learning path, including “Random Numbers with the random Header”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.
What will I learn in “Random Numbers with the random Header”?
Generate quality random numbers with modern engines and distributions. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
How long does the “Random Numbers with the random Header” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Integer vs Floating Point Types
- The cmath Header: pow sqrt abs round
- Random Numbers with the random Header
- Numeric Limits and Overflow Behaviour