Skip to content

Instantly share code, notes, and snippets.

@thejoshwolfe
Created August 2, 2016 19:06
Show Gist options
  • Save thejoshwolfe/8582c60aa6916d7f2e574c1580931feb to your computer and use it in GitHub Desktop.
Save thejoshwolfe/8582c60aa6916d7f2e574c1580931feb to your computer and use it in GitHub Desktop.

Revisions

  1. thejoshwolfe created this gist Aug 2, 2016.
    21 changes: 21 additions & 0 deletions output
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    ...................................................................................................#
    ...............................................................................................#####
    ..........................................................................................##########
    .....................................................................................###############
    ................................................................................####################
    ..........................................................................##########################
    ......................................................................##############################
    .................................................................###################################
    ...........................................................#########################################
    .......................................................#############################################
    ..................................................##################################################
    ............................................########################################################
    .......................................#############################################################
    ...................................#################################################################
    .............................#######################################################################
    .........................###########################################################################
    ...................#################################################################################
    ..............######################################################################################
    ..........##########################################################################################
    .....###############################################################################################
    ####################################################################################################
    31 changes: 31 additions & 0 deletions test.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    var choices = [];
    var itemCount = 100;
    var triangleArea = itemCount * (itemCount + 1) / 2;
    for (var i = 0; i < 1000000; i += 1) {
    var y = Math.random() * triangleArea;
    var index = Math.floor((Math.sqrt(8 * y + 1) - 1) / 2);
    //var index = Math.floor(Math.sqrt(Math.random() * itemCount));
    if (choices[index]) {
    choices[index] += 1;
    } else {
    choices[index] = 1;
    }
    }

    var height = 20;
    var maxValue = 0;
    choices.forEach(function(v) {
    if (v > maxValue) maxValue = v;
    });
    var outputString = "";
    for (var y = height; y >= 0; y--) {
    for (var index = 0; index < itemCount; index++) {
    if (choices[index] / maxValue * height >= y) {
    outputString += "#";
    } else {
    outputString += ".";
    }
    }
    outputString += "\n";
    }
    console.log(outputString);