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 -
//Author: Josh Ibrahim, This program takes the user input for number of rolls and simulates the odds/error of two dice rolls
-
-
#include <iostream>
-
#include <cstdlib>
-
#include <ctime>
-
-
using namespace std;
-
-
int getRoll ();
-
-
void displayTotals (int maxRoll,int numberOfRolls);
-
-
int main( )
-
{
-
//sets the seed for the random number
-
srand(time(0));
-
-
//variables to store user input and dice rolls
-
int numberOfRolls;
-
int maxRoll = 0;
-
-
-
int loop = 1;
-
-
-
cout << "This program rolls two dice and tabulates the results.\n";
-
-
while (loop != 0)
-
{
-
-
-
//ask the user how many rolls of the dice to computer
-
cout << "\nEnter Number of Rolls: ";
-
cin >> numberOfRolls;
-
-
-
cout<<"\nSum #Rolled Odds %Error";
-
-
//for loop calculates odds and %error
-
displayTotals (maxRoll,numberOfRolls);
-
-
-
-
cout << "\nTry again? (1 = Yes, 0 = Exit)";
-
cin >> loop;
-
-
}
-
-
-
return 0;
-
-
}
-
-
-
-
//function takes @numberOfRolls and then calculates possible dice rolls with @maxRoll and displays the outcome. Calls @getRoll
-
void displayTotals (int maxRoll,int numberOfRolls)
-
{
-
-
//counters for each dice roll
-
int countRoll2 = 0, countRoll3 = 0,countRoll4 = 0,countRoll5 = 0,countRoll6 = 0,countRoll7 = 0;
-
int countRoll8 = 0,countRoll9 = 0,countRoll10 = 0,countRoll11= 0,countRoll12= 0;
-
-
-
-
//calculates dice rolll using a switch
-
for ( int i = 0; i < numberOfRolls; i++)
-
{
-
-
//maxRoll calls get roll twice to grab a psuedorandom number between 2-12
-
maxRoll = getRoll()+ getRoll();
-
-
//switch statement counts everytime the "dice" land on a number
-
switch (maxRoll)
-
{
-
case 2: countRoll2++;
-
break;
-
case 3: countRoll3++;
-
break;
-
case 4: countRoll4++;
-
break;
-
case 5: countRoll5++;
-
break;
-
case 6: countRoll6++;
-
break;
-
case 7: countRoll7++;
-
break;
-
case 8: countRoll8++;
-
break;
-
case 9: countRoll9++;
-
break;
-
case 10: countRoll10++;
-
break;
-
case 11: countRoll11++;
-
break;
-
case 12: countRoll12++;
-
break;
-
}
-
-
}
-
-
//displays results in order: number of times rolled, odds, %error
-
//odds are calculated by: number of total outcomes - total number of unfavorable outcomes
-
//error is calculated like probabilities: number of unfavorableoutcomes/totalnumberofoutcomes
-
cout <<"\n2: " << countRoll2<<" "<< (36) - (35)<<" "<< (RAND_MAX - 2)/ static_cast < double > (numberOfRolls);
-
cout <<"\n3: " << countRoll3<<" "<<(36) - (34)<<" "<< (RAND_MAX - 3)/ static_cast < double > (numberOfRolls;
-
cout <<"\n4: " << countRoll4<<" "<<(36) - (33)<<" "<< (RAND_MAX - 4)/ static_cast < double > (numberOfRolls);
-
cout <<"\n5: " << countRoll5<<" "<<(36) - (32)<<" "<< (RAND_MAX - 5)/ static_cast < double > (numberOfRolls);
-
cout <<"\n6: " << countRoll6<<" "<<(36) - (31)<<" "<< (RAND_MAX - 6)/ static_cast < double > (numberOfRolls);
-
cout <<"\n7: " << countRoll7<<" "<<(36) - (30)<<" "<< (RAND_MAX - 7)/ static_cast < double > (numberOfRolls);
-
cout <<"\n8: " << countRoll8<<" "<<(36) - (31)<<" "<< (RAND_MAX - 8)/ static_cast < double > (numberOfRolls);
-
cout <<"\n9: " << countRoll9<<" "<<(36) - (32)<<" "<< (RAND_MAX -9)/ static_cast < double > (numberOfRolls);
-
cout <<"\n10: " << countRoll10<<" "<<(36) - (33)<<" "<< (RAND_MAX - 10)/ static_cast < double > (numberOfRolls);
-
cout <<"\n11: " << countRoll11<<" "<<(36) - (34)<<" "<< (RAND_MAX - 11)/ static_cast < double > (numberOfRolls);
-
cout <<"\n12: " << countRoll12<<" "<<(36) - (35)<<" "<< (RAND_MAX - 12)/ static_cast < double > (numberOfRolls);
-
}
-
-
-
//function simulates a diceroll and outputs a number between 1-6 based on system time
-
int getRoll ()
-
{
-
-
//variable for diceroll
-
int diceRoll;
-
-
//dice roll declaration
-
diceRoll = rand()%6 +1;
-
-
//returns a number between 2-12
-
return diceRoll;
-
}
-
1 5556
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |