Random Engines
Generate random bits.
Random Engines is a free C++ Academy lesson on CoddyKit. This is lesson 1 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 <random>?
The old rand() is low quality and hard to control. The <random> library splits randomness into engines (sources of random bits) and distributions (shapes of output).
#include <iostream>
#include <random>
int main() {
std::mt19937 engine(42);
std::cout << "engine produced a value\n";
unsigned int v = engine();
std::cout << (v != 0 ? "non-zero" : "zero") << '\n';
return 0;
}The Mersenne Twister
std::mt19937 is the most common engine: fast, high quality, with a long period. The number is its state size in bits.
#include <iostream>
#include <random>
int main() {
std::mt19937 gen(1);
unsigned int a = gen();
unsigned int b = gen();
std::cout << (a != b ? "two different values" : "same") << '\n';
return 0;
}Engines Are Deterministic
Given the same seed, an engine always produces the same sequence. This is what makes results reproducible.
#include <iostream>
#include <random>
int main() {
std::mt19937 a(123), b(123);
std::cout << std::boolalpha << (a() == b()) << '\n';
std::cout << (a() == b()) << '\n';
return 0;
}Calling the Engine
An engine is a callable. Each call engine() returns the next raw random integer in its range.
#include <iostream>
#include <random>
int main() {
std::mt19937 gen(7);
for (int i = 0; i < 3; ++i) {
unsigned int v = gen();
std::cout << (v % 100) << ' ';
}
std::cout << '\n';
return 0;
}Engine Range
min() and max() report the range of raw values an engine can emit.
#include <iostream>
#include <random>
int main() {
std::mt19937 gen;
std::cout << "min: " << gen.min() << '\n';
std::cout << "max: " << gen.max() << '\n';
return 0;
}Other Engines
The library offers several engines:
minstd_rand: small linear congruential generator.mt19937: general purpose default.ranlux48: higher quality, slower.
#include <iostream>
#include <random>
int main() {
std::minstd_rand lcg(5);
std::mt19937 mt(5);
std::cout << (lcg() % 10) << ' ' << (mt() % 10) << '\n';
return 0;
}64-bit Engine
Use std::mt19937_64 when you need 64-bit random values, for example for large hashes or huge ranges.
#include <iostream>
#include <random>
int main() {
std::mt19937_64 gen(99);
unsigned long long v = gen();
std::cout << "got a 64-bit value: " << (v != 0 ? "yes" : "no") << '\n';
return 0;
}Engine vs Distribution
The raw engine output is uniform over a huge range. To get useful values like a die roll, feed the engine into a distribution.
#include <iostream>
#include <random>
int main() {
std::mt19937 gen(2024);
std::uniform_int_distribution<int> die(1, 6);
std::cout << "rolled " << die(gen) << '\n';
return 0;
}Discarding Values
discard(n) advances the engine by n steps without producing output, handy for skipping ahead in a sequence.
#include <iostream>
#include <random>
int main() {
std::mt19937 a(10), b(10);
b.discard(2);
a(); a();
std::cout << std::boolalpha << (a() == b()) << '\n';
return 0;
}Reusing One Engine
Create a single engine and reuse it for all randomness in your program. Creating a fresh engine each time is wasteful and can reduce quality.
#include <iostream>
#include <random>
int main() {
std::mt19937 gen(2026);
std::uniform_int_distribution<int> d10(1, 10);
int total = 0;
for (int i = 0; i < 5; ++i) total += d10(gen);
std::cout << "sum of 5 rolls: " << total << '\n';
return 0;
}Default Seed Pitfall
A default-constructed engine uses a fixed default seed, so it produces the same sequence every run. You must seed it for real variety (covered next lesson).
#include <iostream>
#include <random>
int main() {
std::mt19937 gen; // default seed: same every run
std::cout << "first value mod 1000: " << (gen() % 1000) << '\n';
return 0;
}Quick Check
Test your understanding of random engines.
Recap
You learned about random engines:
<random>separates engines (bits) from distributions (shape)std::mt19937is the go-to engine; it is deterministic for a given seed- reuse one engine and seed it properly for real variety
Next, you'll shape engine output using distributions.
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 Engines” lesson free?
Yes — any 3 lessons of the C++ Academy learning path, including “Random Engines”, 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 Engines”?
Generate random bits. 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 1 of 4.
How long does the “Random Engines” 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
- Random Engines
- Distributions
- Seeding Properly
- Practical Examples