Practical Examples
Dice, shuffles, and sampling.
Practical Examples is a free C++ Academy lesson on CoddyKit — lesson 4 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.
Rolling Dice
A six-sided die is a uniform_int_distribution(1, 6) fed by a seeded engine.
#include <iostream>
#include <random>
int main() {
std::mt19937 gen(2026);
std::uniform_int_distribution<int> die(1, 6);
for (int i = 0; i < 5; ++i) std::cout << die(gen) << ' ';
std::cout << '\n';
return 0;
}Summing Two Dice
Rolling two dice and summing them produces the familiar 2 to 12 range with a peak at 7.
#include <iostream>
#include <random>
int main() {
std::mt19937 gen(1);
std::uniform_int_distribution<int> die(1, 6);
int total = die(gen) + die(gen);
std::cout << "two dice sum: " << total << '\n';
return 0;
}Flipping a Coin
A fair coin is a bernoulli_distribution(0.5). Count heads over many flips.
#include <iostream>
#include <random>
int main() {
std::mt19937 gen(7);
std::bernoulli_distribution coin(0.5);
int heads = 0;
for (int i = 0; i < 100; ++i) if (coin(gen)) ++heads;
std::cout << heads << " heads out of 100\n";
return 0;
}Shuffling a Deck
std::shuffle randomly reorders a range using your engine, the modern replacement for the removed random_shuffle.
#include <iostream>
#include <random>
#include <algorithm>
#include <vector>
int main() {
std::vector<int> cards{1, 2, 3, 4, 5};
std::mt19937 gen(3);
std::shuffle(cards.begin(), cards.end(), gen);
for (int c : cards) std::cout << c << ' ';
std::cout << '\n';
return 0;
}Picking a Random Winner
Select one element by generating a random index in range.
#include <iostream>
#include <random>
#include <vector>
#include <string>
int main() {
std::vector<std::string> names{"Ann", "Ben", "Cara", "Dan"};
std::mt19937 gen(9);
std::uniform_int_distribution<int> pick(0, names.size() - 1);
std::cout << "winner: " << names[pick(gen)] << '\n';
return 0;
}Sampling Without Replacement
std::sample draws k distinct elements from a range, perfect for a lottery or quiz selection.
#include <iostream>
#include <random>
#include <algorithm>
#include <vector>
int main() {
std::vector<int> pool{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
std::vector<int> picked;
std::mt19937 gen(4);
std::sample(pool.begin(), pool.end(), std::back_inserter(picked), 3, gen);
for (int x : picked) std::cout << x << ' ';
std::cout << '\n';
return 0;
}Random Password Characters
Index into a character set with a uniform distribution to build a random string.
#include <iostream>
#include <random>
#include <string>
int main() {
const std::string chars = "abcdefghijklmnopqrstuvwxyz";
std::mt19937 gen(13);
std::uniform_int_distribution<int> pick(0, chars.size() - 1);
std::string pw;
for (int i = 0; i < 8; ++i) pw += chars[pick(gen)];
std::cout << pw << '\n';
return 0;
}Weighted Choice
std::discrete_distribution picks indices with given weights, so common outcomes appear more often.
#include <iostream>
#include <random>
int main() {
std::mt19937 gen(6);
std::discrete_distribution<int> d{10, 1, 1}; // index 0 is heaviest
int counts[3] = {0};
for (int i = 0; i < 120; ++i) counts[d(gen)]++;
std::cout << counts[0] << ' ' << counts[1] << ' ' << counts[2] << '\n';
return 0;
}Simulating a Random Walk
Step left or right at random and track the final position, a simple stochastic simulation.
#include <iostream>
#include <random>
int main() {
std::mt19937 gen(2);
std::bernoulli_distribution step(0.5);
int pos = 0;
for (int i = 0; i < 1000; ++i) pos += step(gen) ? 1 : -1;
std::cout << "final position: " << pos << '\n';
return 0;
}Rolling Until a Six
Loop until an event occurs to model trials, here counting rolls needed to get a six.
#include <iostream>
#include <random>
int main() {
std::mt19937 gen(15);
std::uniform_int_distribution<int> die(1, 6);
int rolls = 0;
while (die(gen) != 6) ++rolls;
std::cout << "extra rolls before a six: " << rolls << '\n';
return 0;
}Generating Test Data
Distributions quickly fill a vector with random values for testing algorithms.
#include <iostream>
#include <random>
#include <vector>
int main() {
std::mt19937 gen(21);
std::uniform_int_distribution<int> d(1, 50);
std::vector<int> data(5);
for (int& v : data) v = d(gen);
for (int v : data) std::cout << v << ' ';
std::cout << '\n';
return 0;
}Quick Check
Test your understanding of practical randomness.
Recap
You applied <random> in practice:
- dice, coins, and winners via uniform and bernoulli distributions
std::shuffleto reorder andstd::sampleto draw without replacementdiscrete_distributionfor weighted choices, plus simulations like random walks
You now have a complete toolkit for associative containers, hashing, file I/O, and randomness in C++.
Frequently asked questions
Is the “Practical Examples” lesson free?
Yes — the full text of “Practical Examples” 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 “Practical Examples”?
Dice, shuffles, and sampling. 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 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Practical Examples” 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