C++ Academy · Lesson

Distributions

Shape random output.

Lesson 2 of 413 steps

Distributions is a free C++ Academy lesson on CoddyKit. This is lesson 2 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.

What Distributions Do

A distribution maps an engine's raw bits into values with a desired shape and range. You call it with the engine as an argument.

#include <iostream>
#include <random>

int main() {
    std::mt19937 gen(1);
    std::uniform_int_distribution<int> dist(1, 100);
    std::cout << "value: " << dist(gen) << '\n';
    return 0;
}

Uniform Integer Distribution

std::uniform_int_distribution<int>(a, b) returns integers in the inclusive range [a, b], each equally likely.

#include <iostream>
#include <random>

int main() {
    std::mt19937 gen(5);
    std::uniform_int_distribution<int> die(1, 6);
    for (int i = 0; i < 5; ++i) std::cout << die(gen) << ' ';
    std::cout << '\n';
    return 0;
}

Uniform Real Distribution

std::uniform_real_distribution<double>(a, b) gives floating-point values in [a, b), useful for probabilities.

#include <iostream>
#include <random>
#include <iomanip>

int main() {
    std::mt19937 gen(3);
    std::uniform_real_distribution<double> dist(0.0, 1.0);
    std::cout << std::fixed << std::setprecision(3) << dist(gen) << '\n';
    return 0;
}

Bernoulli Distribution

std::bernoulli_distribution(p) returns true with probability p, modeling a biased coin flip.

#include <iostream>
#include <random>

int main() {
    std::mt19937 gen(8);
    std::bernoulli_distribution coin(0.5);
    for (int i = 0; i < 5; ++i) std::cout << (coin(gen) ? 'H' : 'T');
    std::cout << '\n';
    return 0;
}

Normal Distribution

std::normal_distribution<double>(mean, stddev) produces a bell curve clustered around the mean.

#include <iostream>
#include <random>
#include <iomanip>

int main() {
    std::mt19937 gen(11);
    std::normal_distribution<double> dist(100.0, 15.0);
    std::cout << std::fixed << std::setprecision(1) << dist(gen) << '\n';
    return 0;
}

Reusing a Distribution

Distributions are cheap to call repeatedly. Build one and call it in a loop with the same engine.

#include <iostream>
#include <random>

int main() {
    std::mt19937 gen(20);
    std::uniform_int_distribution<int> d(1, 3);
    int counts[4] = {0};
    for (int i = 0; i < 12; ++i) counts[d(gen)]++;
    std::cout << counts[1] << ' ' << counts[2] << ' ' << counts[3] << '\n';
    return 0;
}

Poisson Distribution

std::poisson_distribution<int>(mean) models counts of rare events, like arrivals per minute.

#include <iostream>
#include <random>

int main() {
    std::mt19937 gen(30);
    std::poisson_distribution<int> dist(4.0);
    for (int i = 0; i < 5; ++i) std::cout << dist(gen) << ' ';
    std::cout << '\n';
    return 0;
}

Distribution Parameters

You can query a distribution's parameters with accessors such as a(), b(), mean(), or stddev().

#include <iostream>
#include <random>

int main() {
    std::uniform_int_distribution<int> d(10, 50);
    std::cout << "range: " << d.a() << " to " << d.b() << '\n';
    return 0;
}

Resetting Distribution State

Some distributions (like normal) cache internal state. reset() clears it so the next value depends only on the engine.

#include <iostream>
#include <random>

int main() {
    std::mt19937 gen(40);
    std::normal_distribution<double> dist(0.0, 1.0);
    dist(gen);
    dist.reset();
    std::cout << "distribution state reset\n";
    return 0;
}

Picking a Random Element

To choose a random array index, use a uniform_int_distribution over 0 to size - 1.

#include <iostream>
#include <random>
#include <string>

int main() {
    std::string colors[] = {"red", "green", "blue"};
    std::mt19937 gen(50);
    std::uniform_int_distribution<int> pick(0, 2);
    std::cout << colors[pick(gen)] << '\n';
    return 0;
}

Estimating Pi

Distributions power Monte Carlo methods. Sampling random points in a square estimates pi.

#include <iostream>
#include <random>
#include <iomanip>

int main() {
    std::mt19937 gen(60);
    std::uniform_real_distribution<double> u(0.0, 1.0);
    int inside = 0, n = 10000;
    for (int i = 0; i < n; ++i) {
        double x = u(gen), y = u(gen);
        if (x * x + y * y <= 1.0) ++inside;
    }
    std::cout << std::fixed << std::setprecision(2) << 4.0 * inside / n << '\n';
    return 0;
}

Quick Check

Test your understanding of distribution ranges.

Recap

You learned about distributions:

  • they shape engine output: uniform_int (inclusive), uniform_real ([a,b)), bernoulli, normal, poisson
  • call them with the engine, e.g. dist(gen), and reuse them in loops
  • they enable picking elements and Monte Carlo estimation

Next, you'll seed engines properly.

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

Yes — any 3 lessons of the C++ Academy learning path, including “Distributions”, 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 “Distributions”?

Shape random output. 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 2 of 4.

How long does the “Distributions” 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