C++ Academy · Lesson

Seeding Properly

Get reproducible randomness.

Lesson 3 of 413 steps

Seeding Properly is a free C++ Academy lesson on CoddyKit. This is lesson 3 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 Is Seeding?

A seed is the starting state of an engine. The same seed reproduces the same sequence; a different seed gives a different one.

#include <iostream>
#include <random>

int main() {
    std::mt19937 a(100), b(200);
    std::cout << std::boolalpha << (a() == b()) << '\n';
    return 0;
}

Fixed Seed for Reproducibility

A constant seed makes results repeatable, which is ideal for tests and debugging.

#include <iostream>
#include <random>

int main() {
    auto run = [](unsigned s) {
        std::mt19937 g(s);
        return g() % 1000;
    };
    std::cout << run(42) << ' ' << run(42) << '\n';
    return 0;
}

std::random_device

std::random_device provides nondeterministic seeds from the operating system. Use it once to seed your engine.

#include <iostream>
#include <random>

int main() {
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<int> d(1, 6);
    std::cout << "a roll: " << d(gen) << '\n';
    return 0;
}

Seed Once, Reuse

Seed the engine a single time at startup. Re-seeding before every draw destroys randomness quality.

#include <iostream>
#include <random>

int main() {
    std::random_device rd;
    std::mt19937 gen(rd());            // seed once
    std::uniform_int_distribution<int> d(1, 100);
    for (int i = 0; i < 3; ++i) std::cout << d(gen) << ' ';
    std::cout << '\n';
    return 0;
}

The Time Seed Pitfall

Seeding with time(nullptr) changes only once per second, so programs started together get the same sequence. Prefer random_device.

#include <iostream>
#include <random>
#include <ctime>

int main() {
    std::mt19937 gen(static_cast<unsigned>(std::time(nullptr)));
    std::cout << "seeded from time (coarse)\n";
    std::cout << (gen() % 100) << '\n';
    return 0;
}

Seeding with seed_seq

An engine has lots of internal state. std::seed_seq spreads several seed values across all of it for better initialization.

#include <iostream>
#include <random>

int main() {
    std::seed_seq seq{1, 2, 3, 4};
    std::mt19937 gen(seq);
    std::cout << (gen() % 1000) << '\n';
    return 0;
}

Combining random_device Values

For high-quality seeding, gather several values from random_device into a seed_seq.

#include <iostream>
#include <random>
#include <array>

int main() {
    std::random_device rd;
    std::array<unsigned, 4> seeds{rd(), rd(), rd(), rd()};
    std::seed_seq seq(seeds.begin(), seeds.end());
    std::mt19937 gen(seq);
    std::cout << "well-seeded engine ready\n";
    std::cout << (gen() % 100) << '\n';
    return 0;
}

Re-seeding Explicitly

You can reset an existing engine with seed(value) to restart its sequence from a known point.

#include <iostream>
#include <random>

int main() {
    std::mt19937 gen(1);
    unsigned first = gen();
    gen.seed(1);             // restart
    std::cout << std::boolalpha << (gen() == first) << '\n';
    return 0;
}

Saving and Restoring State

An engine can be streamed out and back in, letting you save and resume an exact random state.

#include <iostream>
#include <sstream>
#include <random>

int main() {
    std::mt19937 gen(5);
    std::ostringstream save; save << gen;
    unsigned expected = gen();
    std::istringstream load(save.str());
    std::mt19937 restored; load >> restored;
    std::cout << std::boolalpha << (restored() == expected) << '\n';
    return 0;
}

random_device Is Not Reproducible

Because random_device is nondeterministic, never use it when you need to reproduce a run. Use a fixed seed for that.

#include <iostream>
#include <random>

int main() {
    const unsigned FIXED = 2026;
    std::mt19937 gen(FIXED); // reproducible across runs
    std::cout << (gen() % 10000) << '\n';
    return 0;
}

A Reusable Generator

A common pattern wraps a seeded engine behind a small helper so the rest of the code just asks for numbers.

#include <iostream>
#include <random>

int roll(std::mt19937& gen) {
    std::uniform_int_distribution<int> d(1, 6);
    return d(gen);
}

int main() {
    std::mt19937 gen(2026);
    std::cout << roll(gen) << ' ' << roll(gen) << '\n';
    return 0;
}

Quick Check

Test your understanding of seeding.

Recap

You learned proper seeding:

  • a fixed seed gives reproducible runs; std::random_device gives unique ones
  • seed once at startup, not per draw; avoid the coarse time() seed
  • seed_seq spreads entropy across the engine, and engine state can be saved/restored via streams

Next, you'll apply all this to practical examples.

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

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

Get reproducible randomness. 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 3 of 4.

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