Skip to content Skip to sidebar Skip to footer

Distributional Sampling In Node.js

I wonder how it's possible to perform distributional sampling in Node.js. In particular, I have the number of elements, where the i'th value of the arrays is the probability of the

Solution 1:

Trivial method:

function distribute(probs) {
    return function() {
        var r = Math.random();
        var i = 0,
            acc = 0;
        while ((acc += probs[i]) <= r)
            i++;
        return i;
    };
}
var sample = distribute([0.2, 0.3, 0.5]);
sample();
sample();
sample();
…

Post a Comment for "Distributional Sampling In Node.js"