Created
January 12, 2017 21:35
-
-
Save oshea00/6e396465fbb9f213810dbba4ed4dafcc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// flipfunc.c : Flips a coin | |
#include "time.h" | |
#include "stdio.h" | |
#include "stdlib.h" | |
double f(int, int *i, int *hist); | |
// Input: flips - how many times to flip the coin. | |
// Output: | |
// *headsInARow - updates with a count of the longest run of heads in a row. | |
// hist[] - updates a list of counts in a row history. | |
// Returns: odds of heads (should be near 50%) | |
double f(int flips, int *headsInARow, int hist[]) | |
{ | |
int heads = 0; | |
int currentRun = 0; | |
for (int i = 0; i < flips; i++) | |
{ | |
int flip = rand() % 2; | |
if (flip == 1) | |
{ | |
heads++; | |
currentRun += 1; | |
} | |
else | |
{ | |
if (currentRun > 2) | |
{ | |
hist[currentRun]++; | |
*headsInARow = (*headsInARow < currentRun) ? currentRun : *headsInARow; | |
} | |
currentRun = 0; | |
} | |
} | |
if (currentRun) | |
{ | |
*headsInARow = (*headsInARow < currentRun) ? currentRun : *headsInARow; | |
hist[currentRun]++; | |
} | |
return heads / (double) flips; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment