Skip to content

Instantly share code, notes, and snippets.

@tail-call
Created November 1, 2017 09:20
Show Gist options
  • Save tail-call/26ae88c458ba58e5ba0390c24c768ed1 to your computer and use it in GitHub Desktop.
Save tail-call/26ae88c458ba58e5ba0390c24c768ed1 to your computer and use it in GitHub Desktop.
Get normally distributed random value
// From <https://github.com/ondras/rot.js/blob/master/src/rng.js>
// I have no idea how this works
function getNormal (mean, stddev) {
do {
var u = 2*this.getUniform()-1;
var v = 2*this.getUniform()-1;
var r = u*u + v*v;
} while (r > 1 || r == 0);
var gauss = u * Math.sqrt(-2*Math.log(r)/r);
return (mean || 0) + gauss*(stddev || 1);
}
@tail-call
Copy link
Author

From what I get, Box–Muller transform is utilized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment