473,473 Members | 1,963 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Working on a dice Game in C++

3 New Member
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
1 5556
weaknessforcats
9,208 Recognized Expert Moderator Expert
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

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

Similar topics

101
by: Elijah Cardon | last post by:
Let's say I have m dice having n sides, such that n^m is not going to bust int as a datatype. With m=4 and n=6, an outcome might be {2, 5, 1, 2}. What is a good way to represent this in c so that...
0
by: Bree | last post by:
The game requires it not to accept negative numbers. At the moment it isnt, and it is urgent I find the solution asap. So if anyone can help I would much appreciate it. Thanks Bree This is the...
1
by: adridder | last post by:
Locally running a "devside" windows apache php mysql server ... everything works perfectly in both firefox and iexplorer now i install it on a remote server ... tried 3 different ones and i...
1
by: lenest | last post by:
I need help writing a program.... You are to write a python program to accomplish the following: a.. Play a dice game of Craps using a random number generator to simulate the roll of the...
3
by: Fredde | last post by:
Hi i'm creating a dice game where you start off to enter how many rolls you want and then you enter a player name and then the rolls occure. after that you will be able to enter an other name and...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.