Skip to content

Instantly share code, notes, and snippets.

@noniq
Created March 29, 2015 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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 hth_first = 0;
var htt_first = 0;
var success_hth = 0;
var success_htt = 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!;
hth_first += 1;
success_hth += 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 if( flip1 === heads && flip2 === tails && flip3 === tails){ // checking if we math the pattern
// if yes, thenwe've matched our pattern - yay!;
htt_first += 1;
success_htt += 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 HTH " + success_hth / hth_first,10,100);
text("average match time HTT " + success_htt / htt_first,10,130);
text("HTH came first: " + hth_first / trials * 100, 10, 180);
text("HTT came first: " + htt_first / trials * 100, 10, 210);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment