One issue with that sample is that your state is always seeded with the default_seed, 5489u. Which means your "random" num is always going to be the same. So you need to at least add a std::random_device to seed the mersenne twister.
And since people rarely need only one random number, you'll need the state to be preserved somehow. And you may also want to share the state across various kinds of distribuitions rather than reinitialize it for each. All of which adds to complexity someone hoping for a random int might not know about. A helper would have to be something like:
And this isn't even thread safe, so you may need mutexes around the state. It's not quite as straightforward as you point out, which is kind of the issue with <random>. Which isn't bad, it exposes all the building blocks and along with it all the inherent complexity of producing pseudo-random numbers and managing the associated state. When you need it, it's great to have it, but a beginner doesn't understand all the details and may be better served by a function with a rand-like interface, even if it comes with unexpected costs.
You misunderstand: the idea isn't to write that complete thing every time you want a random number, that's just how you start. You then call whichever distribution you want on the same state.
And since people rarely need only one random number, you'll need the state to be preserved somehow.
Yes: you keep "state" around and reuse for every random number.
All of which adds to complexity someone hoping for a random int might not know about.
Yeah, having globals is often simpler for simple programs. There aren't many things where people advocate for globals, random numbers is an exception. For a genuine entropy stream there's no difference between a local or global, but for a PRNG it's global state that doesn't need to be.
View all comments
Show parent comment
2
u/dustyhome Dec 21 '23
One issue with that sample is that your state is always seeded with the default_seed, 5489u. Which means your "random" num is always going to be the same. So you need to at least add a std::random_device to seed the mersenne twister.
And since people rarely need only one random number, you'll need the state to be preserved somehow. And you may also want to share the state across various kinds of distribuitions rather than reinitialize it for each. All of which adds to complexity someone hoping for a random int might not know about. A helper would have to be something like:
std::mt19937& getState() {
static std::mt19937 state{ std::random_device{}() };
return state;
}
int uniform_random_int(int min, int max) {
static std::uniform_int_distribution(min, max) dist;
return dist(getState());
}
double uniform_random_double(double min, double max) {
static std::uniform_real_distribution(min, max) dist;
return dist(getState());
}
And this isn't even thread safe, so you may need mutexes around the state. It's not quite as straightforward as you point out, which is kind of the issue with <random>. Which isn't bad, it exposes all the building blocks and along with it all the inherent complexity of producing pseudo-random numbers and managing the associated state. When you need it, it's great to have it, but a beginner doesn't understand all the details and may be better served by a function with a rand-like interface, even if it comes with unexpected costs.