By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,677 Members | 1,136 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,677 IT Pros & Developers. It's quick & easy.

Working on a dice Game in C++

P: 3
Hello all, I am working on a dice game for my homework in a c++ class. I am suppose to calculate the odds and probability of 2 dice rolls x amount of times and list the probability as rand() error. I have most of the program done but my problem is I can't figure out how to calculate odds and probability for multiple rolls. any suggestions or help would be appreciated
thank you
-Josh Ibrahim
Expand|Select|Wrap|Line Numbers
  1.  //Author: Josh Ibrahim, This program takes the user input for number of rolls and simulates the odds/error of two dice rolls
  2.  
  3.  #include <iostream>
  4.  #include <cstdlib>
  5.  #include <ctime>
  6.  
  7.  using namespace std;
  8.  
  9.  int getRoll ();
  10.  
  11.  void displayTotals (int maxRoll,int numberOfRolls);
  12.  
  13.  int main( )
  14.  {
  15.  //sets the seed for the random number
  16.  srand(time(0));
  17.  
  18.  //variables to store user input and dice rolls
  19.  int  numberOfRolls;
  20.  int maxRoll = 0;
  21.  
  22.  
  23.  int loop = 1;
  24.  
  25.  
  26.  cout << "This program rolls two dice and tabulates the results.\n";
  27.  
  28.  while (loop != 0)
  29.  {
  30.  
  31.  
  32.  //ask the user how many rolls of the dice to computer
  33.  cout << "\nEnter Number of Rolls: ";
  34.  cin >>  numberOfRolls;
  35.  
  36.  
  37.  cout<<"\nSum   #Rolled      Odds   %Error";
  38.  
  39.  //for loop calculates odds and %error
  40. displayTotals (maxRoll,numberOfRolls);
  41.  
  42.  
  43.  
  44.  cout << "\nTry again? (1 = Yes, 0 = Exit)";
  45.  cin >> loop;
  46.  
  47. }
  48.  
  49.  
  50.  return 0;
  51.  
  52.  }
  53.  
  54.  
  55.  
  56.  //function takes @numberOfRolls and then calculates possible dice rolls with @maxRoll and displays the outcome. Calls @getRoll
  57.  void displayTotals (int maxRoll,int numberOfRolls)
  58. {
  59.  
  60.  //counters for each dice roll
  61.  int countRoll2 = 0, countRoll3 = 0,countRoll4 = 0,countRoll5 = 0,countRoll6 = 0,countRoll7 = 0;
  62.  int countRoll8 = 0,countRoll9 = 0,countRoll10 = 0,countRoll11= 0,countRoll12= 0;
  63.  
  64.  
  65.  
  66. //calculates dice rolll using a switch
  67.   for ( int i = 0; i < numberOfRolls; i++)
  68.   {
  69.  
  70.   //maxRoll calls get roll twice to grab a psuedorandom number between 2-12     
  71.   maxRoll = getRoll()+ getRoll();
  72.  
  73.   //switch statement counts everytime the "dice" land on a number
  74.       switch (maxRoll)
  75.       {
  76.       case 2:  countRoll2++;
  77.       break;    
  78.       case 3:  countRoll3++;
  79.       break;    
  80.       case 4:   countRoll4++;
  81.       break;    
  82.       case 5:   countRoll5++;
  83.       break;    
  84.       case 6:  countRoll6++;
  85.       break;    
  86.       case 7:  countRoll7++;
  87.       break;    
  88.       case 8:  countRoll8++;
  89.       break;    
  90.       case 9:  countRoll9++;
  91.       break;
  92.       case 10:  countRoll10++;
  93.       break;
  94.       case 11:  countRoll11++;
  95.       break;
  96.       case 12:  countRoll12++;
  97.       break;
  98.       }
  99.  
  100.   }
  101.  
  102. //displays results in order: number of times rolled, odds, %error  
  103. //odds are calculated by: number of total outcomes - total number of unfavorable outcomes
  104. //error is calculated like probabilities:  number of unfavorableoutcomes/totalnumberofoutcomes
  105.  cout <<"\n2:    " << countRoll2<<"             "<< (36) - (35)<<"       "<< (RAND_MAX - 2)/ static_cast < double > (numberOfRolls);
  106.  cout <<"\n3:    " << countRoll3<<"             "<<(36) -  (34)<<"       "<< (RAND_MAX - 3)/ static_cast < double > (numberOfRolls;
  107.  cout <<"\n4:    " << countRoll4<<"             "<<(36) -  (33)<<"       "<< (RAND_MAX - 4)/ static_cast < double > (numberOfRolls);
  108.  cout <<"\n5:    " << countRoll5<<"             "<<(36) -  (32)<<"       "<< (RAND_MAX - 5)/ static_cast < double > (numberOfRolls);
  109.  cout <<"\n6:    " << countRoll6<<"             "<<(36) -  (31)<<"       "<< (RAND_MAX - 6)/ static_cast < double > (numberOfRolls);
  110.  cout <<"\n7:    " << countRoll7<<"             "<<(36) -  (30)<<"       "<< (RAND_MAX - 7)/ static_cast < double > (numberOfRolls);
  111.  cout <<"\n8:    " << countRoll8<<"             "<<(36) -  (31)<<"       "<< (RAND_MAX - 8)/ static_cast < double > (numberOfRolls);
  112.  cout <<"\n9:    " << countRoll9<<"             "<<(36) -  (32)<<"       "<< (RAND_MAX -9)/ static_cast < double > (numberOfRolls);
  113.  cout <<"\n10:   " << countRoll10<<"             "<<(36) - (33)<<"       "<< (RAND_MAX - 10)/ static_cast < double > (numberOfRolls);
  114.  cout <<"\n11:   " << countRoll11<<"             "<<(36) - (34)<<"       "<< (RAND_MAX - 11)/ static_cast < double > (numberOfRolls);
  115.  cout <<"\n12:   " << countRoll12<<"             "<<(36) - (35)<<"       "<< (RAND_MAX - 12)/ static_cast < double > (numberOfRolls);
  116.  
  117.  
  118. //function simulates a diceroll and outputs a number between 1-6 based on system time
  119. int getRoll ()
  120. {
  121.  
  122.  //variable for diceroll
  123. int diceRoll;
  124.  
  125. //dice roll declaration
  126. diceRoll = rand()%6 +1;
  127.  
  128. //returns a number between 2-12
  129. return diceRoll;
  130. }
  131.  
Sep 19 '14 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
Probability refers to the "good outcomes". In the case of two dice you get a 7 if the dice roll has any of these 6 combinations:

6 1
5 2
4 3
3 4
2 5
1 6

There are 36 ways two dice can come up so the probability of rolling a 7 is 6/36 or 1/6 (good combinations/all combinations). The odds rolling 3 and 4, in that order is 5/1 (bad combinations/good combinations).

Nothing changes on multiple rolls. If you fly a lot the odds of your plane crashing do not change based on the number of flights. Military pilots who believed they had a greater chance of being shot down the more missions they flew were indulging in a myth.
Sep 19 '14 #2

Post your reply

Sign in to post your reply or Sign up for a free account.