Reputation: 23
I am trying to make a mini lottery simulator. I want to generate 8 random numbers with a minimum of 1 and a maximum of 20. And i store these generated numbers in a Lottery object. I have a LotteryManager class, where i can decide how many times do i want to generate these lotteries. But i always get the same random numbers. I seeded the generator so I dont understand whats the problem with it. Any help is appreciated!
This is my lottery class:
class Lottery
{
private:
vector<int> lotteryA;
int lotteryB;
public:
Lottery();
vector<int> getLotteryA() const;
int getLotteryB() const;
void generateLotteryA();
void generateLotteryB();
void printLottery() const;
};
And this is the class that manages it:
class LotteryManager
{
private:
unsigned int quanity = 0;
map<unsigned int, Lottery> lotteries;
public:
LotteryManager();
LotteryManager(unsigned int q);
unsigned int getQuanity() const;
void setQuanity(unsigned int value);
void generateLotteries();
void printLotteries() const;
};
And this is my main code:
LotteryManager* lm = new LotteryManager(100);
lm->generateLotteries();
lm->printLotteries();
The generate and print functions in the LotteryManager class:
void LotteryManager::generateLotteries()
{
for(unsigned int i = 0; i < getQuanity(); ++i)
{
Lottery l;
l.generateLotteryA();
l.generateLotteryB();
lotteries.insert(make_pair(i, l));
l.printLottery();
}
}
void LotteryManager::printLotteries() const
{
cout << "Lotteries:" << endl;
for(auto current : lotteries)
{
cout << current.first << ".: ";
current.second.printLottery();
}
}
And the generate functions in the Lottery class: (The generate A and B are just the different fields, because the game aims to imitate the game named Putto, where you need to guess 8 numbers from 20 in the field A and 1 from 4 in the field B)
void Lottery::generateLotteryA()
{
random_device rn_d;
mt19937 rng(rn_d());
uniform_int_distribution<int> dist(1, 20);
for(unsigned int i = 0; i < 8; ++i)
{
lotteryA.push_back(dist(rng));
}
}
void Lottery::generateLotteryB()
{
srand(time(0));
lotteryB = (rand() % 20) + 1;
}
Thanks in advance!
Upvotes: 2
Views: 102
Reputation: 60218
In Lottery::generateLotteryA
you are declaring a random engine, and random device. Every time you call this function, the engine is seeded with the same random device, and so you could get the same results.
It might seem reasonable that invoking rn_d()
each time with different random_device
s will generate a different value. However, this is not required, and as mentioned in the reference:
... each std::random_device object may generate the same number sequence.
In practice, most implementations will generate different values, and so this is not really a problem. You could even consider it a defect in the standard library for not making this guarantee. However, as currently specified, you could see the same values being produced.
The simplest fix would be to make the engine static
, so it only gets seeded the first time the function is called:
void Lottery::generateLotteryA()
{
// ...
static mt19937 rng(rn_d());
// ...
}
Upvotes: 5