Created
March 7, 2018 04:14
-
-
Save jbobrow/2446097cea90a7f12a8f653aee2773f1 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 | |
| * | |
| * In the show, they mention 14 sets of 7, which is 98 coin flips, with distinct sets | |
| * This is very different than a continuous 100 flips, and much closer to their stated | |
| * 1/6 chance of seeing a perfect run of 7 | |
| */ | |
| int numSets = 14; | |
| int setSize = 7; | |
| int flipsInARow = 7; | |
| int countSuccesses = 0; | |
| int countTotalGoals = 0; | |
| int numberOfSamples = 0; | |
| boolean[][] coinFlips = new boolean[numSets][setSize]; | |
| void setup() { | |
| size(200, 400); | |
| smooth(); | |
| noStroke(); | |
| flipCoins(); | |
| } | |
| void draw() { | |
| int inARowCount = 1; | |
| boolean lastFlip = false; | |
| boolean isAStreak = false; | |
| for (int i=0; i<numSets; i++) { | |
| inARowCount = 0; | |
| for (int j=0; j<setSize; j++) { | |
| boolean isHeads = coinFlips[i][j]; | |
| if (isHeads) { | |
| fill(255); | |
| } else { | |
| fill(0); | |
| } | |
| if (lastFlip == isHeads && isHeads == false) { | |
| inARowCount++; | |
| } else { | |
| inARowCount = 1; | |
| } | |
| lastFlip = isHeads; | |
| if (inARowCount == flipsInARow) { | |
| fill(255, 0, 0); | |
| } | |
| ellipse(20+(width/(setSize+1))*j, 20+(height/(numSets+1))*i, width/20, width/20); | |
| } | |
| } | |
| flipCoins(); // keep running trials | |
| } | |
| void flipCoins() { | |
| int inARowCount = 1; | |
| boolean lastFlip = false; | |
| boolean isAStreak = false; | |
| for (int i=0; i<numSets; i++) { | |
| inARowCount = 0; | |
| for (int j=0; j<setSize; j++) { | |
| boolean flip = random(0, 1) > 0.5; | |
| coinFlips[i][j] = flip; | |
| if (flip == lastFlip && flip == false) { | |
| inARowCount++; | |
| } else { | |
| inARowCount = 1; | |
| } | |
| if (inARowCount == flipsInARow) { | |
| isAStreak = true; | |
| countTotalGoals++; | |
| } | |
| lastFlip = flip; | |
| } | |
| } | |
| if (isAStreak) { | |
| countSuccesses++; | |
| } | |
| numberOfSamples++; | |
| } | |
| void keyPressed() { | |
| //println(keyCode); | |
| if ( keyCode == 32 ) { // SPACEBAR | |
| flipCoins(); | |
| } | |
| if ( keyCode == 80 ) { // P | |
| int percentSuccess = (100*countSuccesses) / numberOfSamples; | |
| int percentGoal = (100*countTotalGoals) / numberOfSamples; | |
| println("So far we have " + countSuccesses + " out of " + numberOfSamples + " samples or a percent of " + percentSuccess + "%"); | |
| println("And in total we have " + countTotalGoals + " in " + numberOfSamples + " samples or a percent of " + percentGoal + "%"); | |
| } | |
| if ( keyCode == 38 ) { // UP ARROW | |
| flipsInARow++; | |
| println("flips in a row: " + flipsInARow); | |
| } | |
| if ( keyCode == 40 ) { // DOWN ARROW | |
| flipsInARow--; | |
| println("flips in a row: " + flipsInARow); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment