|
Last change
on this file since e4c61dd was e4c61dd, checked in by istevanoska <ilinastevanoska@…>, 6 months ago |
|
Prototype 1.1
|
-
Property mode
set to
100644
|
|
File size:
716 bytes
|
| Line | |
|---|
| 1 | import defaultSource from "./defaultSource.js";
|
|---|
| 2 |
|
|---|
| 3 | export default (function sourceRandomNormal(source) {
|
|---|
| 4 | function randomNormal(mu, sigma) {
|
|---|
| 5 | var x, r;
|
|---|
| 6 | mu = mu == null ? 0 : +mu;
|
|---|
| 7 | sigma = sigma == null ? 1 : +sigma;
|
|---|
| 8 | return function() {
|
|---|
| 9 | var y;
|
|---|
| 10 |
|
|---|
| 11 | // If available, use the second previously-generated uniform random.
|
|---|
| 12 | if (x != null) y = x, x = null;
|
|---|
| 13 |
|
|---|
| 14 | // Otherwise, generate a new x and y.
|
|---|
| 15 | else do {
|
|---|
| 16 | x = source() * 2 - 1;
|
|---|
| 17 | y = source() * 2 - 1;
|
|---|
| 18 | r = x * x + y * y;
|
|---|
| 19 | } while (!r || r > 1);
|
|---|
| 20 |
|
|---|
| 21 | return mu + sigma * y * Math.sqrt(-2 * Math.log(r) / r);
|
|---|
| 22 | };
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | randomNormal.source = sourceRandomNormal;
|
|---|
| 26 |
|
|---|
| 27 | return randomNormal;
|
|---|
| 28 | })(defaultSource);
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.