0Pricing
C++ Academy · Lesson

Distributions

Shape random output.

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

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.

Frequently asked questions

Is the “Distributions” lesson free?

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

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