0Pricing
C++ Academy · Lesson

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 — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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 program

Random 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.

Frequently asked questions

Is the “Random Numbers with the random Header” lesson free?

Yes — the full text of “Random Numbers with the random Header” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

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

  1. Integer vs Floating Point Types
  2. The cmath Header: pow sqrt abs round
  3. Random Numbers with the random Header
  4. Numeric Limits and Overflow Behaviour
← Back to C++ Academy