Seeding Properly
Get reproducible randomness.
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;
}All lessons in this course
- Random Engines
- Distributions
- Seeding Properly
- Practical Examples