I just wanted to create a basic RNG class using mersenne twister that can return random numbers of predefined ranges. I came up with the following code:
rng.h
#pragma once
#include <random>
class RNG
{
public:
static float getRand01();
static float getRand11();
static int getRand(const int min, const int max);
private:
static std::random_device rd;
static std::mt19937 generator;
static std::uniform_real_distribution<float> dis01;
static std::uniform_real_distribution<float> dis11;
static std::uniform_int_distribution<int> disAB;
};
rng.cpp
#include "RNG.h"
std::random_device RNG::rd{};
std::mt19937 RNG::generator{ rd() };
std::uniform_real_distribution<float> RNG::dis01{0.0f, 1.0f};
std::uniform_real_distribution<float> RNG::dis11{-1.0f, 1.0f};
std::uniform_int_distribution<int> RNG::disAB{1, 1};
float RNG::getRand01()
{
return dis01(generator);
}
float RNG::getRand11()
{
return dis11(generator);
}
int RNG::getRand(const int min, const int max)
{
disAB.param(std::uniform_int_distribution<int>::param_type(min, max));
return disAB(generator);
}
My main-file looks similar to this
Object someObject;
int main (){ ... }
So someObject is declared in global scope and it makes use of the RNG in its constructor. However the RNG always returns 0 during this time. I think this is because the member are not initialized as they cannot be constant initialised (not sure about the correct terminology here). This is not a big problem, because I can reset the state of someObject in main(). However I am interested if there is a workaround to this while still being able to use the RNG like a "static" class or is this design simply flawed?