Skip to content

Instantly share code, notes, and snippets.

@noniq
Created March 29, 2015 17:10

Revisions

  1. noniq revised this gist Mar 29, 2015. 1 changed file with 23 additions and 10 deletions.
    33 changes: 23 additions & 10 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -3,8 +3,10 @@ var trials = 10000; // Number of times to try the process

    var heads = 1;
    var tails = 0;
    var success = 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
    @@ -30,8 +32,21 @@ for(i = 0; i < trials; i++){

    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
    }

    success += j; // add the step at which the match occurred. Min is j = 3 since
    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
    @@ -56,10 +71,8 @@ for(i = 0; i < trials; i++){
    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);

    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);
  2. noniq created this gist Mar 29, 2015.
    65 changes: 65 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    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);