C++ Academy · Lesson

Practical Examples

Dice, shuffles, and sampling.

Lesson 4 of 413 steps

Practical Examples is a free C++ Academy lesson on CoddyKit. This is lesson 4 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.

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::shuffle to reorder and std::sample to draw without replacement
  • discrete_distribution for weighted choices, plus simulations like random walks

You now have a complete toolkit for associative containers, hashing, file I/O, and randomness in C++.

Free to start

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 “Practical Examples” lesson free?

Yes — any 3 lessons of the C++ Academy learning path, including “Practical Examples”, 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 “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, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

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

  1. Random Engines
  2. Distributions
  3. Seeding Properly
  4. Practical Examples
← Back to C++ Academy