Asks the user for the chance of a coin landing on heads, the number of trials per experiment, and the number of experiments. For example, given 5 trials per experiment and 20 experiments, the program will flip a coin 5 times and record the results 20 times. Then, it displays the results, as well as the theoretical and observed probabilities of each event happening.
I turned this in already but I really enjoyed doing the assignment and I'd like to know how to make it better, faster, etc. I'm pretty new to computer science so I'd like to learn the correct practices and everything. Right off the bat I know that I should pass things by reference instead of by value and that I shouldn't have used global variables.
//Name Lastname
//Class
//Coin Flip Simulation
//LIBRARIES
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <time.h>
#include <cmath>
using namespace std;
//FUNCTION PROTOTYPES
bool generate(); //Performs a coin flip with the given chance
void runExperiment(); //Performs and prints the results of the trials
void getInfo(); //Gets the information to perform the experiment
int choose(int, int); //Performs a combination (nCk)
double probability(int); //Calculates the probability of a coin hitting heads n times (binomial thrm)
//GLOBAL VARIABLES
double chance = 0.5; //Chance of landing on heads
int numTrials = 5; //Number of trials per experiment
int numExperiments = 1000; //Number of experiments
bool printT = false; //Whether to print the results of each trial
int main()
{
srand(time(NULL));
getInfo();
runExperiment();
getchar();
getchar();
return 0;
}
void getInfo()
{
cout << "Please enter the chance of landing on heads (between 0.0 and 1.0, recommended 0.5)" << endl;
cin >> chance;
cout << "Please enter the number of trials per experiment (max 20, recommended 5)" << endl;
cin >> numTrials;
cout << "Please enter the number of experiments (recommended: 1000, max 1 billion)" << endl;
cin >> numExperiments;
cout << "Print the results of each experiment? Takes much longer (0 for no, 1 for yes)" << endl;
cin >> printT;
}
void runExperiment()
{
int m = 0, n = 0, a[22];
for (int b = 0; b < 22; b++)
a[b] = 0;
for (int i = 0; i < numExperiments; i++)
{
int k = 0, l = 0;
for (int j = 0; j < numTrials; j++)
{
if (generate())
k++;
else
l++;
}
if (printT) //Prints experiment results
{
cout << "TRIAL: NUMBER OF HEADS: " << k;
cout << " NUMBER OF TAILS: " << l << endl;
}
for (int p = 0; p < numTrials+1; p++)
{
if (k == p)
a[p]++;
}
m = m + k;
n = n + l;
}
//Prints final information to screen
cout << "-----INFORMATION-----" << endl;
cout << numTrials << " TRIALS PER EXPERIMENT" << endl;
cout << numExperiments << " EXPERIMENTS" << endl;
cout << "CHANCE OF LANDING ON HEADS: " << chance * 100 << "%" << endl;
cout << endl << "-----RESULTS-----" << endl;
cout << "TOTAL HEADS: " << m << endl;
cout << "TOTAL TAILS: " << n << endl;
cout << "PERCENT OF TIMES THE COIN LANDED ON HEADS: " << ((double)m / (m + n))*100 << "%" << endl;
for (int q = 0; q < numTrials+1; q++)
cout << "NUMBER OF EXPERIMENTS WITH " << q << " HEADS AND " << numTrials - q << " TAILS: " << a[q] << endl;
cout << "-----STATS-----" << endl;
double summation = 0;
for (int z = 0; z < numTrials + 1; z++)
summation = summation + a[z];
for (int q = 0; q < numTrials + 1; q++)
cout << "OBSERVED PROBABILITY OF " << q << " HEADS AND " << numTrials - q << " TAILS: " << (a[q]/summation)*100 << "%" << endl;
cout << endl;
for (int q = 0; q < numTrials + 1; q++)
cout << "THEORETICAL PROBABILITY OF " << q << " HEADS AND " << numTrials - q << " TAILS: " << probability(q) << "%" << endl;
}
double probability(int k) //Binomial thrm
{
double f = choose(numTrials, k)*(pow((chance), k))*(pow((1-chance), (numTrials - k)));
f = f * 100;
return f;
}
int choose(int n, int k) //nCk
{
if (k > n)
return 0;
int r = 1;
for (int d = 1; d <= k; ++d)
{
r *= n--;
r /= d;
}
return r;
}
bool generate()
{
double i = rand() % 100;
i = i / 100;
if (i < chance)
return true; //HEADS
else
return false; //TAILS
}
rand()
is known for its non randomness in the last bit. Also since C++14 it has been replaced by a better random number library. Also the random range is not exactly dividable by 100. So numbers from [0-68] are slightly more likely than number [69-99] (assuming random max is 32768) \$\endgroup\$