mardi 17 novembre 2020

Why do I get the same random numbers when i properly seeded my generator?

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!




Aucun commentaire:

Enregistrer un commentaire