(mean, stdev)
| 119275 | const SQRT2 = Math.SQRT2; |
| 119276 | let nextSample = NaN; |
| 119277 | function 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 | } |
| 119296 | function densityNormal(value, mean, stdev) { |
| 119297 | stdev = stdev == null ? 1 : stdev; |
| 119298 | const z = (value - (mean || 0)) / stdev; |
no test coverage detected