Skip to content

Commit 952abef

Browse files
committed
Add d3.random.{logNormal,irwinHall}. Fixes d3#571.
1 parent e42eb90 commit 952abef

File tree

4 files changed

+78
-11
lines changed

4 files changed

+78
-11
lines changed

d3.v2.js

+20-4
Original file line numberDiff line numberDiff line change
@@ -183,17 +183,33 @@
183183
return [ a, c ];
184184
};
185185
d3.random = {
186-
normal: function(mean, deviation) {
187-
if (arguments.length < 2) deviation = 1;
188-
if (arguments.length < 1) mean = 0;
186+
normal: function(µ, σ) {
187+
var n = arguments.length;
188+
if (n < 2) σ = 1;
189+
if (n < 1) µ = 0;
189190
return function() {
190191
var x, y, r;
191192
do {
192193
x = Math.random() * 2 - 1;
193194
y = Math.random() * 2 - 1;
194195
r = x * x + y * y;
195196
} while (!r || r > 1);
196-
return mean + deviation * x * Math.sqrt(-2 * Math.log(r) / r);
197+
return µ + σ * x * Math.sqrt(-2 * Math.log(r) / r);
198+
};
199+
},
200+
logNormal: function(µ, σ) {
201+
var n = arguments.length;
202+
if (n < 2) σ = 1;
203+
if (n < 1) µ = 0;
204+
var random = d3.random.normal();
205+
return function() {
206+
return Math.exp(µ + σ * random());
207+
};
208+
},
209+
irwinHall: function(m) {
210+
return function() {
211+
for (var s = 0, j = 0; j < m; j++) s += Math.random();
212+
return s / m;
197213
};
198214
}
199215
};

d3.v2.min.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core/random.js

+20-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
11
d3.random = {
2-
normal: function(mean, deviation) {
3-
if (arguments.length < 2) deviation = 1;
4-
if (arguments.length < 1) mean = 0;
2+
normal: function(µ, σ) {
3+
var n = arguments.length;
4+
if (n < 2) σ = 1;
5+
if (n < 1) µ = 0;
56
return function() {
67
var x, y, r;
78
do {
89
x = Math.random() * 2 - 1;
910
y = Math.random() * 2 - 1;
1011
r = x * x + y * y;
1112
} while (!r || r > 1);
12-
return mean + deviation * x * Math.sqrt(-2 * Math.log(r) / r);
13+
return µ + σ * x * Math.sqrt(-2 * Math.log(r) / r);
14+
};
15+
},
16+
logNormal: function(µ, σ) {
17+
var n = arguments.length;
18+
if (n < 2) σ = 1;
19+
if (n < 1) µ = 0;
20+
var random = d3.random.normal();
21+
return function() {
22+
return Math.exp(µ + σ * random());
23+
};
24+
},
25+
irwinHall: function(m) {
26+
return function() {
27+
for (var s = 0, j = 0; j < m; j++) s += Math.random();
28+
return s / m;
1329
};
1430
}
1531
};

test/core/random-test.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
require("../env");
2+
3+
var vows = require("vows"),
4+
assert = require("assert");
5+
6+
var suite = vows.describe("d3.random");
7+
8+
suite.addBatch({
9+
"normal": {
10+
"topic": function() {
11+
return d3.random.normal();
12+
},
13+
"returns a number": function(random) {
14+
assert.typeOf(random(), "number");
15+
}
16+
},
17+
"logNormal": {
18+
"topic": function() {
19+
return d3.random.logNormal();
20+
},
21+
"returns a number": function(random) {
22+
assert.typeOf(random(), "number");
23+
}
24+
},
25+
"irwinHall": {
26+
"topic": function() {
27+
return d3.random.irwinHall(10);
28+
},
29+
"returns a number": function(random) {
30+
assert.typeOf(random(), "number");
31+
}
32+
}
33+
});
34+
35+
suite.export(module);

0 commit comments

Comments
 (0)