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

Function sampleNormal

docs/tests/v2/es6/js/sanddance.js:87022–87047  ·  view source on GitHub ↗
(mean, stdev)

Source from the content-addressed store, hash-verified

87020let nextSample = NaN;
87021
87022function sampleNormal(mean, stdev) {
87023 mean = mean || 0;
87024 stdev = stdev == null ? 1 : stdev;
87025 let x = 0,
87026 y = 0,
87027 rds,
87028 c;
87029
87030 if (nextSample === nextSample) {
87031 x = nextSample;
87032 nextSample = NaN;
87033 } else {
87034 do {
87035 x = (0, _random.random)() * 2 - 1;
87036 y = (0, _random.random)() * 2 - 1;
87037 rds = x * x + y * y;
87038 } while (rds === 0 || rds > 1);
87039
87040 c = Math.sqrt(-2 * Math.log(rds) / rds); // Box-Muller transform
87041
87042 x *= c;
87043 nextSample = y * c;
87044 }
87045
87046 return mean + x * stdev;
87047}
87048
87049function densityNormal(value, mean, stdev) {
87050 stdev = stdev == null ? 1 : stdev;

Callers 1

_defaultFunction · 0.70

Calls 1

logMethod · 0.45

Tested by

no test coverage detected