Skip to content

Commit

Permalink
Adopt “random” prefix.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Jan 7, 2016
1 parent c74f2e2 commit e63b930
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 111 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,33 @@ In a vanilla environment, a `d3_random` global is exported. [Try d3-random in yo

## API Reference

<a name="uniform" href="#uniform">#</a> d3_random.<b>uniform</b>([<i>min</i>, ][<i>max</i>])
<a name="uniform" href="#uniform">#</a> d3.<b>randomUniform</b>([<i>min</i>, ][<i>max</i>])

Returns a function for generating random numbers with a [uniform distribution](https://en.wikipedia.org/wiki/Uniform_distribution_\(continuous\)). The minimum allowed value of a returned number is *min*, and the maximum is *max*. If *min* is not specified, it defaults to 0; if *max* is not specified, it defaults to 1. For example:

```js
d3_random.uniform(6)(); // Returns a number greater than or equal to 0 and less than 6.
d3_random.uniform(1, 5)(); // Returns a number greater than or equal to 1 and less than 5.
d3.randomUniform(6)(); // Returns a number greater than or equal to 0 and less than 6.
d3.randomUniform(1, 5)(); // Returns a number greater than or equal to 1 and less than 5.
```

Note that you can also use the built-in [Math.random](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Math/random) to generate uniform distributions directly. For example, to generate a random integer between 0 and 99 (inclusive), you can say `Math.random() * 100 | 0`.

<a name="normal" href="#normal">#</a> d3_random.<b>normal</b>([<i>mu</i>][, <i>sigma</i>])
<a name="normal" href="#normal">#</a> d3.<b>randomNormal</b>([<i>mu</i>][, <i>sigma</i>])

Returns a function for generating random numbers with a [normal (Gaussian) distribution](https://en.wikipedia.org/wiki/Normal_distribution). The expected value of the generated numbers is *mu*, with the given standard deviation *sigma*. If *mu* is not specified, it defaults to 0; if *sigma* is not specified, it defaults to 1.

<a name="logNormal" href="#logNormal">#</a> d3_random.<b>logNormal</b>([<i>mu</i>][, <i>sigma</i>])
<a name="logNormal" href="#logNormal">#</a> d3.<b>randomLogNormal</b>([<i>mu</i>][, <i>sigma</i>])

Returns a function for generating random numbers with a [log-normal distribution](https://en.wikipedia.org/wiki/Log-normal_distribution). The expected value of the random variable’s natural logrithm is *mu*, with the given standard deviation *sigma*. If *mu* is not specified, it defaults to 0; if *sigma* is not specified, it defaults to 1.

<a name="bates" href="#bates">#</a> d3_random.<b>bates</b>(<i>n</i>)
<a name="bates" href="#bates">#</a> d3.<b>randomBates</b>(<i>n</i>)

Returns a function for generating random numbers with a [Bates distribution](https://en.wikipedia.org/wiki/Bates_distribution) with *n* independent variables.

<a name="irwinHall" href="#irwinHall">#</a> d3_random.<b>irwinHall</b>(<i>n</i>)
<a name="irwinHall" href="#irwinHall">#</a> d3.<b>randomIrwinHall</b>(<i>n</i>)

Returns a function for generating random numbers with an [Irwin–Hall distribution](https://en.wikipedia.org/wiki/Irwin–Hall_distribution) with *n* independent variables.

<a name="exponential" href="#exponential">#</a> d3_random.<b>exponential</b>(<i>lambda</i>)
<a name="exponential" href="#exponential">#</a> d3.<b>randomExponential</b>(<i>lambda</i>)

Returns a function for generating random numbers with an [exponential distribution](https://en.wikipedia.org/wiki/Exponential_distribution) with the rate *lambda*; equivalent to time between events in a [Poisson process](https://en.wikipedia.org/wiki/Poisson_point_process) with a mean of 1 / *lambda*. For example, exponential(1/40) generates random times between events where, on average, one event occurs every 40 units of time.
12 changes: 6 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export {default as uniform} from "./src/uniform";
export {default as normal} from "./src/normal";
export {default as logNormal} from "./src/logNormal";
export {default as bates} from "./src/bates";
export {default as irwinHall} from "./src/irwinHall";
export {default as exponential} from "./src/exponential";
export {default as randomUniform} from "./src/uniform";
export {default as randomNormal} from "./src/normal";
export {default as randomLogNormal} from "./src/logNormal";
export {default as randomBates} from "./src/bates";
export {default as randomIrwinHall} from "./src/irwinHall";
export {default as randomExponential} from "./src/exponential";
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "d3-random",
"version": "0.1.1",
"version": "0.2.0",
"description": "Generate random numbers from various distributions.",
"keywords": [
"d3",
Expand All @@ -25,7 +25,7 @@
"prepublish": "npm run test && uglifyjs build/d3-random.js -c -m -o build/d3-random.min.js && rm -f build/d3-random.zip && zip -j build/d3-random.zip -- LICENSE README.md build/d3-random.js build/d3-random.min.js"
},
"devDependencies": {
"d3-array": "~0.6.1",
"d3-array": "~0.7.0",
"faucet": "0.0",
"rollup": "0.20.5",
"seedrandom": "2",
Expand Down
28 changes: 14 additions & 14 deletions test/bates-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var tape = require("tape"),
arrays = require("d3-arrays"),
array = require("d3-array"),
random = require("../"),
skewness = require("./skewness"),
kurtosis = require("./kurtosis");
Expand All @@ -9,35 +9,35 @@ require("./inDelta");

var mathRandom = Math.random;

tape.test("bates(n) returns random numbers with a mean of one-half", function(test) {
tape.test("randomBates(n) returns random numbers with a mean of one-half", function(test) {
Math.seedrandom("f330fbece4c1c99f");
test.inDelta(arrays.mean(arrays.range(10000).map(random.bates(1))), .5, .05);
test.inDelta(arrays.mean(arrays.range(10000).map(random.bates(10))), .5, .05);
test.inDelta(array.mean(array.range(10000).map(random.randomBates(1))), .5, .05);
test.inDelta(array.mean(array.range(10000).map(random.randomBates(10))), .5, .05);
test.end();
});

tape.test("bates(n) returns random numbers with a variance of 1 / (12 * n)", function(test) {
tape.test("randomBates(n) returns random numbers with a variance of 1 / (12 * n)", function(test) {
Math.seedrandom("c4af5ee918417093");
test.inDelta(arrays.variance(arrays.range(10000).map(random.bates(1))), 1 / 12, .05);
test.inDelta(arrays.variance(arrays.range(10000).map(random.bates(10))), 1 / 120, .05);
test.inDelta(array.variance(array.range(10000).map(random.randomBates(1))), 1 / 12, .05);
test.inDelta(array.variance(array.range(10000).map(random.randomBates(10))), 1 / 120, .05);
test.end();
});

tape.test("bates(n) returns random numbers with a skewness of 0", function(test) {
tape.test("randomBates(n) returns random numbers with a skewness of 0", function(test) {
Math.seedrandom("bb0bb470f346ff65");
test.inDelta(skewness(arrays.range(10000).map(random.bates(1))), 0, .05);
test.inDelta(skewness(arrays.range(10000).map(random.bates(10))), 0, .05);
test.inDelta(skewness(array.range(10000).map(random.randomBates(1))), 0, .05);
test.inDelta(skewness(array.range(10000).map(random.randomBates(10))), 0, .05);
test.end();
});

tape.test("bates(n) returns random numbers with a kurtosis of -6 / (5 * n)", function(test) {
tape.test("randomBates(n) returns random numbers with a kurtosis of -6 / (5 * n)", function(test) {
Math.seedrandom("3c21f0c8f5a8332c");
test.inDelta(kurtosis(arrays.range(10000).map(random.bates(1))), -6 / 5, .05);
test.inDelta(kurtosis(arrays.range(10000).map(random.bates(10))), -6 / 50, .05);
test.inDelta(kurtosis(array.range(10000).map(random.randomBates(1))), -6 / 5, .05);
test.inDelta(kurtosis(array.range(10000).map(random.randomBates(10))), -6 / 50, .05);
test.end();
});

tape("bates() [teardown]", function(test) {
tape("randomBates() [teardown]", function(test) {
Math.random = mathRandom;
test.end();
});
12 changes: 6 additions & 6 deletions test/exponential-test.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
var tape = require("tape"),
arrays = require("d3-arrays"),
array = require("d3-array"),
random = require("../");

require("seedrandom");
require("./inDelta");

var mathRandom = Math.random;

tape.test("exponential(lambda) returns random exponentially distributed numbers with a mean of 1/lambda.", function(test) {
tape.test("randomExponential(lambda) returns random exponentially distributed numbers with a mean of 1/lambda.", function(test) {
Math.seedrandom("d5cb594f444fc692");

var mean = 20,
lambda = 1 / mean, // average rate (e.g. 1 per 20 minutes)
times = arrays.range(10000).map(random.exponential(lambda));
times = array.range(10000).map(random.randomExponential(lambda));

test.inDelta(arrays.mean(times), mean, mean * 0.05);
test.inDelta(array.mean(times), mean, mean * 0.05);

// Test cumulative distribution in intervals of 10.
arrays.range(10, 100, 10).forEach(function(elapsed) {
array.range(10, 100, 10).forEach(function(elapsed) {
var within = times.filter(function(t) { return t <= elapsed; }),
expected = 1 - Math.exp(-elapsed * lambda);
test.inDelta(within.length / times.length, expected, expected * 0.02);
Expand All @@ -26,7 +26,7 @@ tape.test("exponential(lambda) returns random exponentially distributed numbers
test.end();
});

tape("exponential() [teardown]", function(test) {
tape("randomExponential() [teardown]", function(test) {
Math.random = mathRandom;
test.end();
});
28 changes: 14 additions & 14 deletions test/irwinHall-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var tape = require("tape"),
arrays = require("d3-arrays"),
array = require("d3-array"),
random = require("../"),
skewness = require("./skewness"),
kurtosis = require("./kurtosis");
Expand All @@ -9,35 +9,35 @@ require("./inDelta");

var mathRandom = Math.random;

tape.test("irwinHall(n) returns random numbers with a mean of n / 2", function(test) {
tape.test("randomIrwinHall(n) returns random numbers with a mean of n / 2", function(test) {
Math.seedrandom("f330fbece4c1c99f");
test.inDelta(arrays.mean(arrays.range(10000).map(random.irwinHall(1))), 1 / 2, .05);
test.inDelta(arrays.mean(arrays.range(10000).map(random.irwinHall(10))), 10 / 2, .05);
test.inDelta(array.mean(array.range(10000).map(random.randomIrwinHall(1))), 1 / 2, .05);
test.inDelta(array.mean(array.range(10000).map(random.randomIrwinHall(10))), 10 / 2, .05);
test.end();
});

tape.test("irwinHall(n) returns random numbers with a variance of n / 12", function(test) {
tape.test("randomIrwinHall(n) returns random numbers with a variance of n / 12", function(test) {
Math.seedrandom("c4af5ee918417093");
test.inDelta(arrays.variance(arrays.range(10000).map(random.irwinHall(1))), 1 / 12, .05);
test.inDelta(arrays.variance(arrays.range(10000).map(random.irwinHall(10))), 10 / 12, .05);
test.inDelta(array.variance(array.range(10000).map(random.randomIrwinHall(1))), 1 / 12, .05);
test.inDelta(array.variance(array.range(10000).map(random.randomIrwinHall(10))), 10 / 12, .05);
test.end();
});

tape.test("irwinHall(n) returns random numbers with a skewness of 0", function(test) {
tape.test("randomIrwinHall(n) returns random numbers with a skewness of 0", function(test) {
Math.seedrandom("bb0bb470f346ff65");
test.inDelta(skewness(arrays.range(10000).map(random.irwinHall(1))), 0, .05);
test.inDelta(skewness(arrays.range(10000).map(random.irwinHall(10))), 0, .05);
test.inDelta(skewness(array.range(10000).map(random.randomIrwinHall(1))), 0, .05);
test.inDelta(skewness(array.range(10000).map(random.randomIrwinHall(10))), 0, .05);
test.end();
});

tape.test("irwinHall(n) returns random numbers with a kurtosis of -6 / (5 * n)", function(test) {
tape.test("randomIrwinHall(n) returns random numbers with a kurtosis of -6 / (5 * n)", function(test) {
Math.seedrandom("3c21f0c8f5a8332c");
test.inDelta(kurtosis(arrays.range(10000).map(random.irwinHall(1))), -6 / 5, .05);
test.inDelta(kurtosis(arrays.range(10000).map(random.irwinHall(10))), -6 / 50, .05);
test.inDelta(kurtosis(array.range(10000).map(random.randomIrwinHall(1))), -6 / 5, .05);
test.inDelta(kurtosis(array.range(10000).map(random.randomIrwinHall(10))), -6 / 50, .05);
test.end();
});

tape("irwinHall() [teardown]", function(test) {
tape("randomIrwinHall() [teardown]", function(test) {
Math.random = mathRandom;
test.end();
});
10 changes: 5 additions & 5 deletions test/kurtosis.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
var arrays = require("d3-arrays");
var array = require("d3-array");

module.exports = function(array) {
var mean = arrays.mean(array),
module.exports = function(numbers) {
var mean = array.mean(numbers),
sum4 = 0,
sum2 = 0,
v,
i = -1,
n = array.length;
n = numbers.length;

while (++i < n) {
v = array[i] - mean;
v = numbers[i] - mean;
sum2 += v * v;
sum4 += v * v * v * v;
}
Expand Down
34 changes: 17 additions & 17 deletions test/logNormal-test.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
var tape = require("tape"),
arrays = require("d3-arrays"),
array = require("d3-array"),
random = require("../");

require("seedrandom");
require("./inDelta");

var mathRandom = Math.random;

tape.test("logNormal() returns random numbers with a log-mean of zero", function(test) {
tape.test("randomLogNormal() returns random numbers with a log-mean of zero", function(test) {
Math.seedrandom("a22ebc7c488a3a47");
test.inDelta(arrays.mean(arrays.range(10000).map(random.logNormal()), Math.log), 0, .05);
test.inDelta(array.mean(array.range(10000).map(random.randomLogNormal()), Math.log), 0, .05);
test.end();
});

tape.test("logNormal() returns random numbers with a log-standard deviation of one", function(test) {
tape.test("randomLogNormal() returns random numbers with a log-standard deviation of one", function(test) {
Math.seedrandom("06fd26b46c25607e");
test.inDelta(arrays.deviation(arrays.range(10000).map(random.logNormal()), Math.log), 1, .05);
test.inDelta(array.deviation(array.range(10000).map(random.randomLogNormal()), Math.log), 1, .05);
test.end();
});

tape.test("logNormal(mu) returns random numbers with the specified log-mean", function(test) {
tape.test("randomLogNormal(mu) returns random numbers with the specified log-mean", function(test) {
Math.seedrandom("fffe77600db5c1ad");
test.inDelta(arrays.mean(arrays.range(10000).map(random.logNormal(42)), Math.log), 42, .05);
test.inDelta(arrays.mean(arrays.range(10000).map(random.logNormal(-2)), Math.log), -2, .05);
test.inDelta(array.mean(array.range(10000).map(random.randomLogNormal(42)), Math.log), 42, .05);
test.inDelta(array.mean(array.range(10000).map(random.randomLogNormal(-2)), Math.log), -2, .05);
test.end();
});

tape.test("logNormal(mu) returns random numbers with a log-standard deviation of 1", function(test) {
tape.test("randomLogNormal(mu) returns random numbers with a log-standard deviation of 1", function(test) {
Math.seedrandom("9caf2156de45315a");
test.inDelta(arrays.deviation(arrays.range(10000).map(random.logNormal(42)), Math.log), 1, .05);
test.inDelta(arrays.deviation(arrays.range(10000).map(random.logNormal(-2)), Math.log), 1, .05);
test.inDelta(array.deviation(array.range(10000).map(random.randomLogNormal(42)), Math.log), 1, .05);
test.inDelta(array.deviation(array.range(10000).map(random.randomLogNormal(-2)), Math.log), 1, .05);
test.end();
});

tape.test("logNormal(mu, sigma) returns random numbers with the specified log-mean and log-standard deviation", function(test) {
tape.test("randomLogNormal(mu, sigma) returns random numbers with the specified log-mean and log-standard deviation", function(test) {
Math.seedrandom("c0d761f591fb5e43");
test.inDelta(arrays.mean(arrays.range(10000).map(random.logNormal(42, 2)), Math.log), 42, .05);
test.inDelta(arrays.mean(arrays.range(10000).map(random.logNormal(-2, 2)), Math.log), -2, .05);
test.inDelta(arrays.deviation(arrays.range(10000).map(random.logNormal(42, 2)), Math.log), 2, .05);
test.inDelta(arrays.deviation(arrays.range(10000).map(random.logNormal(-2, 2)), Math.log), 2, .05);
test.inDelta(array.mean(array.range(10000).map(random.randomLogNormal(42, 2)), Math.log), 42, .05);
test.inDelta(array.mean(array.range(10000).map(random.randomLogNormal(-2, 2)), Math.log), -2, .05);
test.inDelta(array.deviation(array.range(10000).map(random.randomLogNormal(42, 2)), Math.log), 2, .05);
test.inDelta(array.deviation(array.range(10000).map(random.randomLogNormal(-2, 2)), Math.log), 2, .05);
test.end();
});

tape("logNormal() [teardown]", function(test) {
tape("randomLogNormal() [teardown]", function(test) {
Math.random = mathRandom;
test.end();
});
34 changes: 17 additions & 17 deletions test/normal-test.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
var tape = require("tape"),
arrays = require("d3-arrays"),
array = require("d3-array"),
random = require("../");

require("seedrandom");
require("./inDelta");

var mathRandom = Math.random;

tape.test("normal() returns random numbers with a mean of zero", function(test) {
tape.test("randomNormal() returns random numbers with a mean of zero", function(test) {
Math.seedrandom("a22ebc7c488a3a47");
test.inDelta(arrays.mean(arrays.range(10000).map(random.normal())), 0, .05);
test.inDelta(array.mean(array.range(10000).map(random.randomNormal())), 0, .05);
test.end();
});

tape.test("normal() returns random numbers with a standard deviation of one", function(test) {
tape.test("randomNormal() returns random numbers with a standard deviation of one", function(test) {
Math.seedrandom("06fd26b46c25607e");
test.inDelta(arrays.deviation(arrays.range(10000).map(random.normal())), 1, .05);
test.inDelta(array.deviation(array.range(10000).map(random.randomNormal())), 1, .05);
test.end();
});

tape.test("normal(mu) returns random numbers with the specified mean", function(test) {
tape.test("randomNormal(mu) returns random numbers with the specified mean", function(test) {
Math.seedrandom("fffe77600db5c1ad");
test.inDelta(arrays.mean(arrays.range(10000).map(random.normal(42))), 42, .05);
test.inDelta(arrays.mean(arrays.range(10000).map(random.normal(-2))), -2, .05);
test.inDelta(array.mean(array.range(10000).map(random.randomNormal(42))), 42, .05);
test.inDelta(array.mean(array.range(10000).map(random.randomNormal(-2))), -2, .05);
test.end();
});

tape.test("normal(mu) returns random numbers with a standard deviation of 1", function(test) {
tape.test("randomNormal(mu) returns random numbers with a standard deviation of 1", function(test) {
Math.seedrandom("9caf2156de45315a");
test.inDelta(arrays.deviation(arrays.range(10000).map(random.normal(42))), 1, .05);
test.inDelta(arrays.deviation(arrays.range(10000).map(random.normal(-2))), 1, .05);
test.inDelta(array.deviation(array.range(10000).map(random.randomNormal(42))), 1, .05);
test.inDelta(array.deviation(array.range(10000).map(random.randomNormal(-2))), 1, .05);
test.end();
});

tape.test("normal(mu, sigma) returns random numbers with the specified mean and standard deviation", function(test) {
tape.test("randomNormal(mu, sigma) returns random numbers with the specified mean and standard deviation", function(test) {
Math.seedrandom("c0d761f591fb5e43");
test.inDelta(arrays.mean(arrays.range(10000).map(random.normal(42, 2))), 42, .05);
test.inDelta(arrays.mean(arrays.range(10000).map(random.normal(-2, 2))), -2, .05);
test.inDelta(arrays.deviation(arrays.range(10000).map(random.normal(42, 2))), 2, .05);
test.inDelta(arrays.deviation(arrays.range(10000).map(random.normal(-2, 2))), 2, .05);
test.inDelta(array.mean(array.range(10000).map(random.randomNormal(42, 2))), 42, .05);
test.inDelta(array.mean(array.range(10000).map(random.randomNormal(-2, 2))), -2, .05);
test.inDelta(array.deviation(array.range(10000).map(random.randomNormal(42, 2))), 2, .05);
test.inDelta(array.deviation(array.range(10000).map(random.randomNormal(-2, 2))), 2, .05);
test.end();
});

tape("normal() [teardown]", function(test) {
tape("randomNormal() [teardown]", function(test) {
Math.random = mathRandom;
test.end();
});
10 changes: 5 additions & 5 deletions test/skewness.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
var arrays = require("d3-arrays");
var array = require("d3-array");

module.exports = function(array) {
var mean = arrays.mean(array),
module.exports = function(numbers) {
var mean = array.mean(numbers),
sum3 = 0,
sum2 = 0,
v,
i = -1,
n = array.length;
n = numbers.length;

while (++i < n) {
v = array[i] - mean;
v = numbers[i] - mean;
sum2 += v * v;
sum3 += v * v * v;
}
Expand Down
Loading

0 comments on commit e63b930

Please sign in to comment.