MCPcopy Create free account
hub / github.com/microsoft/SandDance / sampleNormal

Function sampleNormal

docs/app/js/sanddance-app.js:119277–119295  ·  view source on GitHub ↗
(mean, stdev)

Source from the content-addressed store, hash-verified

119275const SQRT2 = Math.SQRT2;
119276let nextSample = NaN;
119277function sampleNormal(mean, stdev) {
119278 mean = mean || 0;
119279 stdev = stdev == null ? 1 : stdev;
119280 let x = 0, y = 0, rds, c;
119281 if (nextSample === nextSample) {
119282 x = nextSample;
119283 nextSample = NaN;
119284 } else {
119285 do {
119286 x = random() * 2 - 1;
119287 y = random() * 2 - 1;
119288 rds = x * x + y * y;
119289 }while (rds === 0 || rds > 1);
119290 c = Math.sqrt(-2 * Math.log(rds) / rds); // Box-Muller transform
119291 x *= c;
119292 nextSample = y * c;
119293 }
119294 return mean + x * stdev;
119295}
119296function densityNormal(value, mean, stdev) {
119297 stdev = stdev == null ? 1 : stdev;
119298 const z = (value - (mean || 0)) / stdev;

Callers 2

gaussianFunction · 0.70
sampleLogNormalFunction · 0.70

Calls 2

randomFunction · 0.70
logMethod · 0.45

Tested by

no test coverage detected