high I am generating a program that randomizes a dice throw. The random
number generator to simulate die throws, and will investigate the tendency of experimental
probabilities to get closer to the theoretical probabilities as the number of trials is increased.
This is what my output is supposed to look like
Die face Count Expt Prob Math Prob Difference
——– —– ——— ——— ———-
1 10054 0.167567 0.166667 0.000900
2 10003 0.166717 0.166667 0.000050
3 10071 0.167850 0.166667 0.001183
4 9878 0.164633 0.166667 0.002033
5 10092 0.168200 0.166667 0.001533
6 9902 0.165033 0.166667 0.001633
and this is what i have so far
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <cmath>
using namespace std;
int coinToss() {
return rand()%2;
} // end coinToss
int main() {
cout << fixed << setprecision(6);
// output identification
cout << "CPS 150 Sample (Coin toss probability study)\n\n";
// seed the random number generator with a random(!) value
srand(time(0));
int numTosses;// number of coin tosses
int tossResult;// 0 = head, 1 = tail
const int HEAD = 0, TAIL = 1;
int numHeads = 0, numTails = 0;// counters
// prompt for and read the number of coin tosses
cout << "Enter number of coin tosses: "; cin >> numTosses;
// toss coin repeatedly and count numbers of heads and tails
for (int k = 1; k <= numTosses; k++) {
tossResult = coinToss();
if (tossResult == HEAD)
numHeads++;
else// it must be a tail, right?
numTails++;
} // end for
// calculate experimental probabilities
double exptProbHead = static_cast<double>(numHeads)/numTosses;
double exptProbTail = static_cast<double>(numTails)/numTosses;
const double mathProb = 0.5;// theoretical probability of
// getting a head or a tail
// Display results
cout << "\nNumber of coin tosses = " << numTosses << endl;
cout << "Number of Heads = " << numHeads << endl;
cout << "Experimental probability for Heads = " << exptProbHead << endl;
cout << "Difference with Theoretical probability = "
<< fabs(mathProb – exptProbHead) << endl;
cout << "Number of Tails = " << numTails << endl;
cout << "Experimental probability for Tails = " << exptProbTail << endl;
cout << "Difference with Theoretical probability = "
<< fabs(mathProb – exptProbTail) << endl;
cout << "\nThat’s all folks\n\n";
return 0;
}
PLEASE PLEASE HELP…. thanks
Technorati Tags: cmath, coin toss probability, coin tosses, cout, cps, cstdlib, ctime, face count, getting a head, heads and tails, iomanip, iostream, lt, probabilities, quot, random number generator, random value, tendency, theoretical probability, using namespace std