0

So I have a project for my class where I have to make a simulated craps game run 1000 times and display the number of wins and losses. I ran into a problem that I cannot seem to solve where the game runs but has the same numbers for the rolling of the dice. I tried a few things like putting it in a while loop (it was in a for loop before) as apparently for loops do not reset the variables. I tried to "reset" the variable by making it equal to 0 at the beginning of the loop. However this didn't work and any help is appreciated (I'm new and messy when programming, sorry).

int onek (void){
    int total = 0;
    int wins = 0;
    int count = 0;
    while(count <= 1000) {
            srand(time(NULL));
            int ad = 1 + rand() % 6;
            int bd = 1 + rand() % 6;
            int point = ad + bd;
            cout << ad << "    " << bd << "    " << point << "    "
                 << wins << "    "<< total << endl;

            if (point == 7 || point == 11) {
                cout << "Win!" << endl;
                total += 1;
                wins += 1;
            } else if (point == 2 || point == 3 || point == 12) {
                cout << "Loss." << endl;
                total += 1;
            } else {    
                int od = 1 + rand() % 6;
                int td = 1 + rand() % 6;
                int sum = od + td;
                if (sum == point) {
                    cout << "Win!" << endl;
                    wins += 1;
                    total += 1;
                } else if (sum == 7) {
                    cout << "Loss." << endl;
                    total += 1;
                }
            }
            count++;
    }

    cout << endl << endl << endl << endl << endl 
         << "Total wins: " << wins << "\nTotal losses: " 
         << total - wins;
    cout << "\nTotal Games: " << total; 
}
Ryan Haining
  • 35,360
  • 15
  • 114
  • 174
Muskratis
  • 19
  • 3
  • 1
    srand(time(NULL)); should be out of loop. And which variable are you trying to reset... – Omid CompSCI Jan 31 '20 at 02:56
  • Trying to reset ab, bd, od, and td which all represent dice – Muskratis Jan 31 '20 at 02:59
  • 1
    call `srand(time(NULL))` in your `main`, once, delete it from inside the `while` loop. `srand` "seeds" the random number generator, if you reseed it over and over with the same value, you will get the same "random" sequences out of `rand()`. – Ryan Haining Jan 31 '20 at 02:59
  • 2
    Also `std::rand` is not generally a great source of random numbers; if you get a chance you may want to look into [C++'s random number facilities.](https://en.cppreference.com/w/cpp/numeric/random) – N. Shead Jan 31 '20 at 03:00

0 Answers0