Trying to find a somewhat clean design for sampling billions of dice rolls and while my first ugly solution did work by just creating the generators for the known ranges (dice) it was an ugly mess of multiple typed out by hand copies only changing the range, and switching which to answer the common roll() method with.
#include <random>
#include <map>
namespace dnd {
class Dice {
private:
class RandomNumberGenerator {
public:
RandomNumberGenerator(int from, int to) {
distr = std::uniform_int_distribution(from, to);
}
private:
std::random_device rand_dev;
std::mt19937 generator = std::mt19937();
std::uniform_int_distribution<int> distr;
public:
int roll() {
return distr(generator);
}
};
static std::map<int, RandomNumberGenerator> generators;
public:
static int dN(int n) {
if (!generators.contains(n)) {
generators[n] = RandomNumberGenerator(1, n);
}
return generators[n].roll();
}
};
Is this construct a proper use of a private class, and how do I put its instances in that map? I suppose something is already wrong with the constructor, despite MSVS not complaining? Not sure if even the basic idea is sound (in C++).
I am trying to move from only creating some generators and create them at program start to create every one I need, as using
public:
template<typename T>
static T random(T range_from, T range_to) {
std::random_device rand_dev;
std::mt19937 generator(rand_dev());
std::uniform_int_distribution<T> distr(range_from, range_to);
return distr(generator);
}
static int d4() {
static std::random_device rand_dev;
static std::mt19937 generator(rand_dev());
static std::uniform_int_distribution<int> distr(1, 4);
return distr(generator);
}
``` ... d20.
is way to slow for the dice not prepared and ugly code wise preparing the expected by hand.