0

My code is the following:

#include <iostream>
#include <vector>
#include <ctime>
#include <algorithm> 

void printVector(const std::vector<int>& v) {
    std::cout << "[";
    for (int i = 0; i < v.size(); i++) {
        //cout << " The element " << i << " is " << v[i] << endl;
        if (i < v.size() - 1)
            std::cout << v[i] << ", ";
        else
            std::cout << v[i];
    }
    std::cout << "]" << std::endl << std::endl;
}


void chooseMainNumbers(std::vector<int>& mainNumbers)
{
    std::srand((unsigned int)(std::time(0)));
    std::random_shuffle(mainNumbers.begin(), mainNumbers.end());
    mainNumbers.resize(5); //getting mainNumbers to only have the first 5 numbers
}

void chooseLuckyStars(std::vector<int>& luckyStars)
{
    std::srand((unsigned int)(std::time(0)));
    std::random_shuffle(luckyStars.begin(), luckyStars.end());
    luckyStars.resize(2); //getting luckyStars to only have the first 2 numbers
}

void getIntersection(std::vector<int>& user, std::vector<int>& key)
{
    std::sort(user.begin(), user.end());
    std::sort(key.begin(), key.end());
    std::vector<int>::iterator it = std::set_intersection(user.begin(), user.end(), key.begin(), key.end(), user.begin());
    user.resize(it - user.begin());
}

int main()
{
    std::vector<std::vector<int>> userMainNumbers, userLuckyStars;
    std::vector<int> keyMainNumbers, keyLuckyStars;
    int N;
    std::cout << "Value of N? ";
    std::cin >> N;
    std::cout << std::endl;
    for (int i = 1; i <= N; i++)
    {
        std::vector<int> tempMainNumbers, tempLuckyStars;

        for (int j = 1; j <= 50; j++)
        {
            if (i == N)
            {
                if (j <=50)
                {
                    keyMainNumbers.push_back(j);
                }
                if (j <= 10)
                {
                    keyLuckyStars.push_back(j);
                }
            }
            if (j <= 50)
            {
                tempMainNumbers.push_back(j);
            }
            if (j <= 10)
            {
                tempLuckyStars.push_back(j);
            }
        }

        chooseMainNumbers(tempMainNumbers);
        chooseLuckyStars(tempLuckyStars);
        userMainNumbers.push_back(tempMainNumbers);
        userLuckyStars.push_back(tempLuckyStars);
        tempMainNumbers.clear();
        tempLuckyStars.clear();
    }

    chooseMainNumbers(keyMainNumbers);
    chooseLuckyStars(keyLuckyStars);
    std::cout << "Key main mumbers: ";
    printVector(keyMainNumbers);
    std::cout << "Key lucky Stars: ";
    printVector(keyLuckyStars);

    std::cout << "---------------------------------------------------------------------------" << std::endl << std::endl;

    for (int i = 0; i < userMainNumbers.size(); i++)
    {
        std::cout << i + 1 << " - Main mumbers: ";
        printVector(userMainNumbers[i]);
        getIntersection(userMainNumbers[i], keyMainNumbers);
        std::cout << "The intersection has: " << userMainNumbers[i].size() << " elements." << std::endl;
        printVector(userMainNumbers[i]);
        std::cout << i + 1 << " - Lucky Stars: ";
        printVector(userLuckyStars[i]);
        getIntersection(userLuckyStars[i], keyLuckyStars);
        std::cout << "The intersection has: " << userLuckyStars[i].size() << " elements." << std::endl;
        printVector(userLuckyStars[i]);
    }

    return 0;
}

I'm basically creating a program to simulate a EuroMillions bet, where I create N bets, where N is given by user, and then create a key, after that I compare all N bets with the key and print to the console the intersection from both. But I'm getting a error and can't figure it out. When I'm on debug mode the code will work right, but if I'm running it directly it will always give the same vector for userMainNumbers and keyMainNumbers / userLuckyStars and keyLuckyStars and consequently always give a perfect intersection.

  • 10
    Pop quiz: what do you think happens when you call `srand(time(NULL))` twice within the same second? – Sam Varshavchik May 27 '20 at 13:55
  • It will give the same result, right? So, i presume, its giving the same result without debugging because program runs in the same second. @SamVarshavchik – qwerty124 May 27 '20 at 14:06
  • Try remove all `srand`s and see if it's better, – Jabberwocky May 27 '20 at 14:07
  • Well, while waiting for me to reply, what was preventing you from removing all of them, and just adding one call in `main()`, and then seeing if that made any difference? – Sam Varshavchik May 27 '20 at 14:12
  • @SamVarshavchik I kinda didn't know very well how the srand() worked, so I just added it every time I was going to use a random function, but I think now I got the point of adding it just one time. – qwerty124 May 27 '20 at 14:18
  • Does this answer your question? [srand() — why call it only once?](https://stackoverflow.com/questions/7343833/srand-why-call-it-only-once) – Jabberwocky May 27 '20 at 14:22

0 Answers0