I have a class dieClass
to represent a six-sided die, and I use <random>
to set the number. Here is the header file die.h
:
#include <random>
#include <iostream>
class dieClass
{
public:
dieClass();
int getNum() const { return num; }
private:
int num;
static std::uniform_int_distribution<int> distribution;
};
and here is the implementation file die.cpp
:
#include "die.h"
dieClass::dieClass()
{
static std::random_device rdevice{};
static std::default_random_engine generator{rdevice()};
num = distribution(generator);
}
std::uniform_int_distribution<int> dieClass::distribution{1, 6};
Question: If I call dieClass dice[5]
, have I created five default_random_engines
, or just one because it is static? Basically, what would be the most efficient way to construct millions of dice? Can I declare generator
outside of the constructor, like I did for distribution
? I don't fully understand what private and static do.
EDIT: Rearranging things like this seems to achieve what I want, although it may not be best practice. I pulled all random-number-generating code from the class and stuck it in the implementation file. Now I can call generator
from a function roll
.
dieClass.h
#include <iostream>
class dieClass
{
public:
die();
void roll();
int getNum() const { return num; }
private:
int num;
};
dieClass.cpp
#include <random>
#include "die.hpp"
std::random_device rdevice{};
std::default_random_engine generator{rdevice()};
std::uniform_int_distribution<int> distribution{1, 6};
dieClass::dieClass()
{
num = distribution(generator);
}
void dieClass::roll()
{
num = distribution(generator);
}