Need help with a dice probability.
Feb 27, 2016 at 10:05pm Feb 27, 2016 at 10:05pm UTC
Here is what I have so far:
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
double toss;
double rem;
double prob1 ;
double prob2;
double prob3;
double prob4;
double prob5;
double prob6;
int count1 = 0;
int count2 = 0;
int count3 = 0;
int count4 = 0;
int count5 = 0;
int count6 = 0;
srand(time(NULL));
cout << "Choose an amount of tosses you would like to try: " ;
while (cin >> toss){
for (int i = 0; i < toss; i++){
rem = rand()%6+1;
if (rem == 1){
count1++;
}
else if (rem == 2){
count2++;
}
else if (rem == 3){
count3++;
}
else if (rem == 4){
count4++;
}
else if (rem == 5){
count5++;
}
else if (rem == 6){
count6++;
}
else {
cout << "\nSomething went wrong, sorry.\n" ;
}
}
//These probability variables are getting the percentage for each--
//side of the die. I'm thinking my problem lies around here.
prob1 = count1/toss;
cout << "\nProbability for 1: " << prob1 << "%" ;
prob2 = count2/toss;
cout << "\nProbability for 2: " << prob2 << "%" ;
prob3 = count3/toss;
cout << "\nProbability for 3: " << prob3 << "%" ;
prob4 = count4/toss;
cout << "\nProbability for 4: " << prob4 << "%" ;
prob5 = count5/toss;
cout << "\nProbability for 5: " << prob5 << "%" ;
prob6 = count6/toss;
cout << "\nProbability for 6: " << prob6 << "%" ;
}
}
I want to input a number of tosses, then calculate how many times each side of the die was chosen. Im using a random number generator, but im not too sure I have everything correct. I feel like im using way too many variables. We haven't learned how to use arrays in my class, so we're stuck with for loops at the moment.
I would really appreciate some help in understanding this. Thanks in advance!
Feb 28, 2016 at 12:34am Feb 28, 2016 at 12:34am UTC
I figured out my problem. I wasn't multiplying the probabilities by 100 to get the percentages I desired. Here is the fixed code.
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
using namespace std;
int main()
{
double toss;
double rem;
double prob1 ;
double prob2;
double prob3;
double prob4;
double prob5;
double prob6;
int count1 = 0;
int count2 = 0;
int count3 = 0;
int count4 = 0;
int count5 = 0;
int count6 = 0;
srand(time(NULL));
cout << "Choose an amount of tosses you would like to try: " ;
while (cin >> toss){
for (int i = 0; i < toss; i++){
rem = rand()%6+1;
if (rem == 1){
count1++;
}
else if (rem == 2){
count2++;
}
else if (rem == 3){
count3++;
}
else if (rem == 4){
count4++;
}
else if (rem == 5){
count5++;
}
else if (rem == 6){
count6++;
}
else {
cout << "\nSomething went wrong, sorry.\n" ;
}
}
prob1 = (count1/toss)*100;
cout << fixed << setprecision(2) << "\nProbability for 1: " << prob1 << "%" ;
prob2 = (count2/toss)*100;
cout << "\nProbability for 2: " << prob2 << "%" ;
prob3 = (count3/toss)*100;
cout << "\nProbability for 3: " << prob3 << "%" ;
prob4 = (count4/toss)*100;
cout << "\nProbability for 4: " << prob4 << "%" ;
prob5 = (count5/toss)*100;
cout << "\nProbability for 5: " << prob5 << "%" ;
prob6 = (count6/toss)*100;
cout << "\nProbability for 6: " << prob6 << "%" ;
}
}
Last edited on Feb 28, 2016 at 12:34am Feb 28, 2016 at 12:34am UTC
Topic archived. No new replies allowed.