Skip to content

Instantly share code, notes, and snippets.

@noniq
Created March 29, 2015 17:10
Show Gist options
  • Save noniq/13e5587f8f45bb49f677 to your computer and use it in GitHub Desktop.
Save noniq/13e5587f8f45bb49f677 to your computer and use it in GitHub Desktop.
HTT and HTH spin-off
var trials = 10000; // Number of times to try the process
var heads = 1;
var tails = 0;
var success = 0;
var flip1 = 0; // heads will be 1 and tails will be 0;
var flip2 = 0; // same
var flip3 = 0;
var i = 0;
var j = 0;
var FLIP_ROUND = 3;
var Round_Count = 0;
for(i = 0; i < trials; i++){
flip1 = floor(random(0,1) + 0.5); // this formula takes a random number between 0 and 1
flip2 = floor(random(0,1) + 0.5); // and transforms it to either 0 or 1.
for(j = 3; j < 1000; j++)
{
flip3 = floor(random(0,1) + 0.5); // same formula from above
if( flip1 === heads && flip2 === tails && flip3 === heads){ // checking if we math the pattern
// if yes, thenwe've matched our pattern - yay!;
success += j; // add the step at which the match occurred. Min is j = 3 since
// the pattern is 3 flips long.
if(j === FLIP_ROUND){ // counting the number of times we see our sequence first on
Round_Count++; // a specific round
}
j = 10000; // lazy way to break out of the inner for loop when we match the pattern
}
else{
flip1 = flip2; // on to the next flip - just slide down the list
flip2 = flip3;
}
}
}
// printing out the final information.
fill(0,0,0);
textSize(27);
//text("How many total trials -> " + trials,10,50);
//text("average match time " + success / trials,10,100);
text("Number of times on Round " + FLIP_ROUND,10,250);
text(" equals " + Round_Count,10,300);
text(" as a percent " + 100*Round_Count / trials,10,350);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment