nest for loops and random numbers

I am teaching myself C++ and decided to make a dice program. The program will ask the user for the number of dice they want and how many sides on the dice. For instance, I could specify 6 die with 5 sides each. It all works pretty well except it gives me the same random number for each dice roll. Lines 43-47(the for loop) is where the problem lies. Any help or advice would be appreciated. I cant seem to find a solution that works for me and I have been trying to figure it out for a week. Should I be using a nested for loop? vector?

//Dice Roller 2.0

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace std;

int main()
{


int numberDie;
int playerDie, sidesDie;
string yesNo;



srand(time(0)); //seed random number generator based on time
int randomNumber = rand(); //generates random number


do
{

cout <<"How many sides to the die?\n";
cin >> sidesDie;

cout <<"How many die?\n";
cin >> playerDie;

if (playerDie > 99)
{
cout << "\n\nYou can only have a maximum of 99 die.";
cout << "\nHow many die?\n";
cin >> playerDie;
}



for (int i = 0; i < playerDie; i++)
{
numberDie = (randomNumber % sidesDie) + 1;
cout << "\nYour " << (i + 1) << " rolled a " << numberDie << ".\n" << endl;
}






cout <<"Would you like to roll again?\n\n";
cin >> yesNo;



} while (yesNo == "yes" || yesNo == "Yes" || yesNo == "yEs" || yesNo ==
"yeS" ||yesNo == "YeS" || yesNo == "YEs" || yesNo == "yES"
|| yesNo == "YES");


cout << "Cya next time!\n\n\n";


//keeping the screen open
cout <<"Press Enter Key to Exit";
cin.ignore(std::cin.rdbuf()->in_avail() +4);

return 0;


}





Minor fixes at the line where you start doing the rolls and added something to simplify your dowhile loop condition at the end.

You needed to call rand each loop, otherwise you only got one number.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//Dice Roller 2.0

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace std;

int main()
{
  int numberDie;
  int playerDie, sidesDie;
  string yesNo;

  srand(time(0)); //seed random number generator based on time

  do
  {

    cout <<"How many sides to the die?\n";
    cin >> sidesDie;

    cout <<"How many die?\n";
    cin >> playerDie;

    if (playerDie > 99)
    {
      cout << "\n\nYou can only have a maximum of 99 die.";
      cout << "\nHow many die?\n";
      cin >> playerDie;
    }

    int randomNumber; // Move this inside the loop your getting the numbers from!
    for (int i = 0; i < playerDie; i++)
    {
      randomNumber = rand(); //generates random number // Now makes a new random number each loop.
      numberDie = (randomNumber % sidesDie) + 1;
      cout << "\nYour " << (i + 1) << " rolled a " << numberDie << ".\n" << endl;
    }

    cout <<"Would you like to roll again?\n\n";
    cin >> yesNo;
    
    for (int i = 0; i < yesNo.length(); ++i) // Make it easier for your condition below
      yesNo[i] = tolower(yesNo[i]);

  } while (yesNo == "yes");

  cout << "Cya next time!\n\n\n";

  //keeping the screen open
  cout <<"Press Enter Key to Exit";
  cin.ignore(std::cin.rdbuf()->in_avail() +4);

  return 0;
}
Last edited on
Doh! Seems obvious now. Many thanks for the help.
Topic archived. No new replies allowed.