Hacker News new | ask | show | jobs
by __herson__ 4861 days ago
Always 2.7~ with 10,000 trials

    function sumToOne() {
        var acum = 0,
            count = 0;
        do {
            count++;
            acum += Math.random();
        } while (acum <= 1)
        return count;
    }

    var avg = 0,
        nTimes = 10000;

    for (var x = 0; x < nTimes; x++) {
        avg += sumToOne();
    }

    console.log(avg / nTimes);