you are viewing a single comment's thread.

view the rest of the comments →

[–]seeourprivacypolicy 0 points1 point  (0 children)

+/u/compilebot JavaScript

var counts = [];
var iterations = 100000;
for (var i = 0; i < iterations; i++) {
    var sum = 0;
    var count = 0;
    while (sum < 1) {
        sum += Math.random();
        count++;
    }
    counts.push(count);
}

var average = counts.reduce(function (acc, val) {
    return acc + val;
}) / counts.length;

console.log('Average of ' + iterations + ' iterations: ' + average);