Skip to content
Discussion options

You must be logged in to vote

This function was added in version 7.1, so you would need to upgrade to use it.

However, this is a pretty small function to just add to your project. This function, from this StackOverflow answer, will generate a random sample from a normal distribution with mean 0 and standard deviation 1.

// Standard Normal variate using Box-Muller transform.
function randn_bm() {
    var u = 0, v = 0;
    while(u === 0) u = Math.random(); //Converting [0,1) to (0,1)
    while(v === 0) v = Math.random();
    return Math.sqrt( -2.0 * Math.log( u ) ) * Math.cos( 2.0 * Math.PI * v );
}

If you want to be able to set the mean and std then this will let you do that:

function sampleNormal(mean, standard_deviation

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by jodeleeuw
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants