Skip to content

Instantly share code, notes, and snippets.

@eduardo-matos
Created June 18, 2021 17:09
Show Gist options
  • Save eduardo-matos/0a4a82c429ffdac7e9d8791c8755251d to your computer and use it in GitHub Desktop.
Save eduardo-matos/0a4a82c429ffdac7e9d8791c8755251d to your computer and use it in GitHub Desktop.
Gaussian random value
// Source: https://stackoverflow.com/a/49434653/650656
function randn_bm(min, max, skew) {
let u = 0, v = 0;
while(u === 0) u = Math.random() //Converting [0,1) to (0,1)
while(v === 0) v = Math.random()
let num = Math.sqrt( -2.0 * Math.log( u ) ) * Math.cos( 2.0 * Math.PI * v )
num = num / 10.0 + 0.5 // Translate to 0 -> 1
if (num > 1 || num < 0)
num = randn_bm(min, max, skew) // resample between 0 and 1 if out of range
else{
num = Math.pow(num, skew) // Skew
num *= max - min // Stretch to fill range
num += min // offset to min
}
return num
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment