I don't intend on implementing this into an application yet as I'm going to test it first. As such, it doesn't do much beyond rolling. It also overloads some "typical" operators such as ==
and !=
.
Most of all, I'm still very unsure about my random number generator. I've read about <random>
's std::uniform_int_distribution
and that it has advantages over std::rand
, but I'm also not sure if I've put it in a good place. Client code will be forced to use my seed if my source file is used, but my header file is free from this.
What could be said about my RNG use? Anything else that could be done?
Dice.h
#ifndef DICE_H
#define DICE_H
#include <ostream>
class Dice
{
private:
int topValue; // same default type as std::uniform_int_distribution<>
public:
void roll();
int value() const { return topValue; }
};
// the C++ FAQ on SO suggests that these overloads are best as non-members
inline bool operator==(Dice const& lhs, Dice const& rhs) { return lhs.value() == rhs.value(); }
inline bool operator!=(Dice const& lhs, Dice const& rhs) { return !operator==(lhs, rhs); }
inline std::ostream& operator<<(std::ostream& out, Dice const& obj) { return out << obj.value(); }
#endif
Dice.cpp
#include "Dice.h"
#include <random>
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(1, 6);
void Dice::roll()
{
topValue = dis(gen);
}