Skip to content

Instantly share code, notes, and snippets.

@Eraden
Created August 28, 2019 20:58
Show Gist options
  • Save Eraden/537209e2b6014c542a9f389d08c006a1 to your computer and use it in GitHub Desktop.
Save Eraden/537209e2b6014c542a9f389d08c006a1 to your computer and use it in GitHub Desktop.
function randn_bm(min, max, skew) {
var 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
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