Last active
March 6, 2018 03:22
-
-
Save jbobrow/6134796f98c9b611439a3406c84bff23 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
* RadioLab coin flip | |
* | |
* Shows empirically that flipping 100 times and seeing a | |
* streak of 7 is much more likely than stated in the Stochacity episode | |
*/ | |
int numberOfTrials = 0; // number of trials (does what it says :) | |
int numberOfFlipsInTrial = 100; // number of flips in a trial... yeah aptly named | |
int lengthOfStreak = 7; // how many Tails in a row to make a streak | |
int countSuccessfulTrials = 0; // Total number of trials with at least one streak | |
int countStreaks = 0; // Total number of streaks in all trials | |
int countTrials = 0; // Total number of trials | |
ArrayList coinFlips; | |
void setup() { | |
size(200, 200); | |
smooth(); // cuz I like aesthetics | |
noStroke(); // still aesthetic | |
coinFlips = new ArrayList(); | |
flipCoins(); // perform the first trial of flips | |
} | |
void draw() { | |
int inARowCount = 1; | |
boolean resultOfLastFlip = false; // note I use false as Tails and true as Heads... just seemed simple to use a boolean here | |
for (int i=0; i<10; i++) { | |
for (int j=0; j<10; j++) { | |
boolean isHeads = (boolean)coinFlips.get(i*10+j); | |
if (isHeads) { | |
fill(255); | |
} else { | |
fill(0); | |
} | |
if (resultOfLastFlip == isHeads && isHeads == false) { | |
inARowCount++; | |
} else { | |
inARowCount = 1; | |
} | |
resultOfLastFlip = isHeads; | |
if (inARowCount == lengthOfStreak) { | |
fill(255, 0, 0); | |
} | |
ellipse(20+(width/11)*j, 20+(height/11)*i, width/20, height/20); | |
} | |
} | |
flipCoins(); | |
} | |
void flipCoins() { | |
coinFlips.clear(); | |
int inARowCount = 1; | |
boolean resultOfLastFlip = false; | |
boolean isAStreak = false; | |
for (int i=0; i<numberOfFlipsInTrial; i++) { | |
boolean flip = random(0, 1) > 0.5; | |
coinFlips.add(flip); | |
if (flip == resultOfLastFlip && flip == false) { | |
inARowCount++; | |
} else { | |
inARowCount = 1; | |
} | |
if (inARowCount == lengthOfStreak) { | |
isAStreak = true; | |
countStreaks++; | |
} | |
resultOfLastFlip = flip; | |
} | |
if (isAStreak) { | |
countSuccessfulTrials++; | |
} | |
countTrials++; | |
} | |
void keyPressed() { | |
//println(keyCode); | |
if ( keyCode == 32 ) { // SPACEBAR | |
flipCoins(); | |
} | |
if ( keyCode == 80 ) { // P | |
int percentSuccess = (100*countSuccessfulTrials) / countTrials; | |
int percentGoal = (100*countStreaks) / countTrials; | |
println("So far we have " + countSuccessfulTrials + " out of " + countTrials + " samples or a percent of " + percentSuccess + "%"); | |
println("And in total we have " + countStreaks + " in " + countTrials + " samples or a percent of " + percentGoal + "%"); | |
} | |
if ( keyCode == 38 ) { // UP ARROW | |
lengthOfStreak++; | |
println("length of streak: " + lengthOfStreak); | |
} | |
if ( keyCode == 40 ) { // DOWN ARROW | |
lengthOfStreak--; | |
println("length of streak: " + lengthOfStreak); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment