pax_global_header 0000666 0000000 0000000 00000000064 13102410767 0014513 g ustar 00root root 0000000 0000000 52 comment=e080dc81f00fa4cf1965b7edeb33ebcd6e411b04
d3-random-1.1.0/ 0000775 0000000 0000000 00000000000 13102410767 0013276 5 ustar 00root root 0000000 0000000 d3-random-1.1.0/.eslintrc 0000664 0000000 0000000 00000000163 13102410767 0015122 0 ustar 00root root 0000000 0000000 parserOptions:
sourceType: module
extends:
"eslint:recommended"
rules:
no-cond-assign: 0
semi: 2
d3-random-1.1.0/.gitignore 0000664 0000000 0000000 00000000100 13102410767 0015255 0 ustar 00root root 0000000 0000000 *.sublime-workspace
.DS_Store
build/
node_modules
npm-debug.log
d3-random-1.1.0/.npmignore 0000664 0000000 0000000 00000000036 13102410767 0015274 0 ustar 00root root 0000000 0000000 *.sublime-*
build/*.zip
test/
d3-random-1.1.0/LICENSE 0000664 0000000 0000000 00000002703 13102410767 0014305 0 ustar 00root root 0000000 0000000 Copyright 2010-2016 Mike Bostock
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the author nor the names of contributors may be used to
endorse or promote products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
d3-random-1.1.0/README.md 0000664 0000000 0000000 00000011144 13102410767 0014556 0 ustar 00root root 0000000 0000000 # d3-random
Generate random numbers from various distributions.
## Installing
If you use NPM, `npm install d3-random`. Otherwise, download the [latest release](https://github.com/d3/d3-random/releases/latest). You can also load directly from [d3js.org](https://d3js.org), either as a [standalone library](https://d3js.org/d3-random.v1.min.js) or as part of [D3 4.0](https://github.com/d3/d3). AMD, CommonJS, and vanilla environments are supported. In vanilla, a `d3` global is exported:
```html
```
[Try d3-random in your browser.](https://runkit.com/npm/d3-random)
## API Reference
# d3.randomUniform([min, ][max]) [<>](https://github.com/d3/d3-random/blob/master/src/uniform.js "Source")
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.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`.
# d3.randomNormal([mu][, sigma]) [<>](https://github.com/d3/d3-random/blob/master/src/normal.js "Source")
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.
# d3.randomLogNormal([mu][, sigma]) [<>](https://github.com/d3/d3-random/blob/master/src/logNormal.js "Source")
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 logarithm 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.
# d3.randomBates(n) [<>](https://github.com/d3/d3-random/blob/master/src/bates.js "Source")
Returns a function for generating random numbers with a [Bates distribution](https://en.wikipedia.org/wiki/Bates_distribution) with *n* independent variables.
# d3.randomIrwinHall(n) [<>](https://github.com/d3/d3-random/blob/master/src/irwinHall.js "Source")
Returns a function for generating random numbers with an [Irwin–Hall distribution](https://en.wikipedia.org/wiki/Irwin–Hall_distribution) with *n* independent variables.
# d3.randomExponential(lambda) [<>](https://github.com/d3/d3-random/blob/master/src/exponential.js "Source")
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.
# random.source(source)
Returns the same type of function for generating random numbers but where the given random number generator *source* is used as the source of randomness instead of Math.random. The given random number generator must implement the same interface as Math.random and only return values in the range [0, 1). This is useful when a seeded random number generator is preferable to Math.random. For example:
```js
var d3 = require("d3-random"),
seedrandom = require("seedrandom"),
random = d3.randomNormal.source(seedrandom("a22ebc7c488a3a47"))(0, 1);
random(); // 0.9744193494813501
```
d3-random-1.1.0/d3-random.sublime-project 0000664 0000000 0000000 00000000271 13102410767 0020110 0 ustar 00root root 0000000 0000000 {
"folders": [
{
"path": ".",
"file_exclude_patterns": [
"*.sublime-workspace"
],
"folder_exclude_patterns": [
"build"
]
}
]
}
d3-random-1.1.0/index.js 0000664 0000000 0000000 00000000532 13102410767 0014743 0 ustar 00root root 0000000 0000000 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";
d3-random-1.1.0/package.json 0000664 0000000 0000000 00000002647 13102410767 0015575 0 ustar 00root root 0000000 0000000 {
"name": "d3-random",
"version": "1.1.0",
"description": "Generate random numbers from various distributions.",
"keywords": [
"d3",
"d3-module",
"random",
"rng"
],
"homepage": "https://d3js.org/d3-random/",
"license": "BSD-3-Clause",
"author": {
"name": "Mike Bostock",
"url": "http://bost.ocks.org/mike"
},
"main": "build/d3-random.js",
"module": "index",
"jsnext:main": "index",
"repository": {
"type": "git",
"url": "https://github.com/d3/d3-random.git"
},
"scripts": {
"pretest": "rm -rf build && mkdir build && rollup --banner \"$(preamble)\" -f umd -n d3 -o build/d3-random.js -- index.js",
"test": "tape 'test/**/*-test.js' && eslint index.js src",
"prepublish": "npm run test && uglifyjs --preamble \"$(preamble)\" build/d3-random.js -c -m -o build/d3-random.min.js",
"postpublish": "git push && git push --tags && cd ../d3.github.com && cp ../d3-random/build/d3-random.js d3-random.v1.js && cp ../d3-random/build/d3-random.min.js d3-random.v1.min.js && git add d3-random.v1.js d3-random.v1.min.js && git commit -m \"d3-random ${npm_package_version}\" && git push && cd - && zip -j build/d3-random.zip -- LICENSE README.md build/d3-random.js build/d3-random.min.js"
},
"devDependencies": {
"d3-array": "1",
"eslint": "3",
"package-preamble": "0.1",
"rollup": "0.41",
"seedrandom": "2",
"tape": "4",
"uglify-js": "^2.8.11"
}
}
d3-random-1.1.0/src/ 0000775 0000000 0000000 00000000000 13102410767 0014065 5 ustar 00root root 0000000 0000000 d3-random-1.1.0/src/bates.js 0000664 0000000 0000000 00000000567 13102410767 0015531 0 ustar 00root root 0000000 0000000 import defaultSource from "./defaultSource";
import irwinHall from "./irwinHall";
export default (function sourceRandomBates(source) {
function randomBates(n) {
var randomIrwinHall = irwinHall.source(source)(n);
return function() {
return randomIrwinHall() / n;
};
}
randomBates.source = sourceRandomBates;
return randomBates;
})(defaultSource);
d3-random-1.1.0/src/defaultSource.js 0000664 0000000 0000000 00000000066 13102410767 0017232 0 ustar 00root root 0000000 0000000 export default function() {
return Math.random();
}
d3-random-1.1.0/src/exponential.js 0000664 0000000 0000000 00000000511 13102410767 0016746 0 ustar 00root root 0000000 0000000 import defaultSource from "./defaultSource";
export default (function sourceRandomExponential(source) {
function randomExponential(lambda) {
return function() {
return -Math.log(1 - source()) / lambda;
};
}
randomExponential.source = sourceRandomExponential;
return randomExponential;
})(defaultSource);
d3-random-1.1.0/src/irwinHall.js 0000664 0000000 0000000 00000000531 13102410767 0016353 0 ustar 00root root 0000000 0000000 import defaultSource from "./defaultSource";
export default (function sourceRandomIrwinHall(source) {
function randomIrwinHall(n) {
return function() {
for (var sum = 0, i = 0; i < n; ++i) sum += source();
return sum;
};
}
randomIrwinHall.source = sourceRandomIrwinHall;
return randomIrwinHall;
})(defaultSource);
d3-random-1.1.0/src/logNormal.js 0000664 0000000 0000000 00000000625 13102410767 0016360 0 ustar 00root root 0000000 0000000 import defaultSource from "./defaultSource";
import normal from "./normal";
export default (function sourceRandomLogNormal(source) {
function randomLogNormal() {
var randomNormal = normal.source(source).apply(this, arguments);
return function() {
return Math.exp(randomNormal());
};
}
randomLogNormal.source = sourceRandomLogNormal;
return randomLogNormal;
})(defaultSource);
d3-random-1.1.0/src/normal.js 0000664 0000000 0000000 00000001311 13102410767 0015707 0 ustar 00root root 0000000 0000000 import defaultSource from "./defaultSource";
export default (function sourceRandomNormal(source) {
function randomNormal(mu, sigma) {
var x, r;
mu = mu == null ? 0 : +mu;
sigma = sigma == null ? 1 : +sigma;
return function() {
var y;
// If available, use the second previously-generated uniform random.
if (x != null) y = x, x = null;
// Otherwise, generate a new x and y.
else do {
x = source() * 2 - 1;
y = source() * 2 - 1;
r = x * x + y * y;
} while (!r || r > 1);
return mu + sigma * y * Math.sqrt(-2 * Math.log(r) / r);
};
}
randomNormal.source = sourceRandomNormal;
return randomNormal;
})(defaultSource);
d3-random-1.1.0/src/uniform.js 0000664 0000000 0000000 00000000670 13102410767 0016105 0 ustar 00root root 0000000 0000000 import defaultSource from "./defaultSource";
export default (function sourceRandomUniform(source) {
function randomUniform(min, max) {
min = min == null ? 0 : +min;
max = max == null ? 1 : +max;
if (arguments.length === 1) max = min, min = 0;
else max -= min;
return function() {
return source() * max + min;
};
}
randomUniform.source = sourceRandomUniform;
return randomUniform;
})(defaultSource);
d3-random-1.1.0/test/ 0000775 0000000 0000000 00000000000 13102410767 0014255 5 ustar 00root root 0000000 0000000 d3-random-1.1.0/test/bates-test.js 0000664 0000000 0000000 00000003113 13102410767 0016664 0 ustar 00root root 0000000 0000000 var tape = require("tape"),
seedrandom = require("seedrandom"),
d3 = Object.assign({}, require("../"), require("d3-array")),
skewness = require("./skewness"),
kurtosis = require("./kurtosis");
require("./inDelta");
tape.test("d3.randomBates(n) returns random numbers with a mean of one-half", function(test) {
var randomBates = d3.randomBates.source(seedrandom("f330fbece4c1c99f"));
test.inDelta(d3.mean(d3.range(10000).map(randomBates(1))), 0.5, 0.05);
test.inDelta(d3.mean(d3.range(10000).map(randomBates(10))), 0.5, 0.05);
test.end();
});
tape.test("d3.randomBates(n) returns random numbers with a variance of 1 / (12 * n)", function(test) {
var randomBates = d3.randomBates.source(seedrandom("c4af5ee918417093"));
test.inDelta(d3.variance(d3.range(10000).map(randomBates(1))), 1 / 12, 0.05);
test.inDelta(d3.variance(d3.range(10000).map(randomBates(10))), 1 / 120, 0.05);
test.end();
});
tape.test("d3.randomBates(n) returns random numbers with a skewness of 0", function(test) {
var randomBates = d3.randomBates.source(seedrandom("bb0bb470f346ff65"));
test.inDelta(skewness(d3.range(10000).map(randomBates(1))), 0, 0.05);
test.inDelta(skewness(d3.range(10000).map(randomBates(10))), 0, 0.05);
test.end();
});
tape.test("d3.randomBates(n) returns random numbers with a kurtosis of -6 / (5 * n)", function(test) {
var randomBates = d3.randomBates.source(seedrandom("3c21f0c8f5a8332c"));
test.inDelta(kurtosis(d3.range(10000).map(randomBates(1))), -6 / 5, 0.05);
test.inDelta(kurtosis(d3.range(10000).map(randomBates(10))), -6 / 50, 0.05);
test.end();
});
d3-random-1.1.0/test/exponential-test.js 0000664 0000000 0000000 00000001606 13102410767 0020121 0 ustar 00root root 0000000 0000000 var tape = require("tape"),
seedrandom = require("seedrandom"),
d3 = Object.assign({}, require("../"), require("d3-array"));
require("./inDelta");
tape.test("d3.randomExponential(lambda) returns random exponentially distributed numbers with a mean of 1/lambda.", function(test) {
var randomExponential = d3.randomExponential.source(seedrandom("d5cb594f444fc692"));
var mean = 20,
lambda = 1 / mean, // average rate (e.g. 1 per 20 minutes)
times = d3.range(10000).map(randomExponential(lambda));
test.inDelta(d3.mean(times), mean, mean * 0.05);
// Test cumulative distribution in intervals of 10.
d3.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);
});
test.end();
});
d3-random-1.1.0/test/inDelta.js 0000664 0000000 0000000 00000000466 13102410767 0016201 0 ustar 00root root 0000000 0000000 var tape = require("tape");
tape.Test.prototype.inDelta = function(actual, expected, delta) {
this._assert(expected - delta <= actual && actual <= expected + delta, {
message: "should be in delta",
operator: "inDelta",
actual: actual,
expected: [expected - delta, expected + delta]
});
};
d3-random-1.1.0/test/irwinHall-test.js 0000664 0000000 0000000 00000003227 13102410767 0017525 0 ustar 00root root 0000000 0000000 var tape = require("tape"),
seedrandom = require("seedrandom"),
d3 = Object.assign({}, require("../"), require("d3-array")),
skewness = require("./skewness"),
kurtosis = require("./kurtosis");
require("./inDelta");
tape.test("d3.randomIrwinHall(n) returns random numbers with a mean of n / 2", function(test) {
var randomIrwinHall = d3.randomIrwinHall.source(seedrandom("f330fbece4c1c99f"));
test.inDelta(d3.mean(d3.range(10000).map(randomIrwinHall(1))), 1 / 2, 0.05);
test.inDelta(d3.mean(d3.range(10000).map(randomIrwinHall(10))), 10 / 2, 0.05);
test.end();
});
tape.test("d3.randomIrwinHall(n) returns random numbers with a variance of n / 12", function(test) {
var randomIrwinHall = d3.randomIrwinHall.source(seedrandom("c4af5ee918417093"));
test.inDelta(d3.variance(d3.range(10000).map(randomIrwinHall(1))), 1 / 12, 0.05);
test.inDelta(d3.variance(d3.range(10000).map(randomIrwinHall(10))), 10 / 12, 0.05);
test.end();
});
tape.test("d3.randomIrwinHall(n) returns random numbers with a skewness of 0", function(test) {
var randomIrwinHall = d3.randomIrwinHall.source(seedrandom("bb0bb470f346ff65"));
test.inDelta(skewness(d3.range(10000).map(randomIrwinHall(1))), 0, 0.05);
test.inDelta(skewness(d3.range(10000).map(randomIrwinHall(10))), 0, 0.05);
test.end();
});
tape.test("d3.randomIrwinHall(n) returns random numbers with a kurtosis of -6 / (5 * n)", function(test) {
var randomIrwinHall = d3.randomIrwinHall.source(seedrandom("3c21f0c8f5a8332c"));
test.inDelta(kurtosis(d3.range(10000).map(randomIrwinHall(1))), -6 / 5, 0.05);
test.inDelta(kurtosis(d3.range(10000).map(randomIrwinHall(10))), -6 / 50, 0.05);
test.end();
});
d3-random-1.1.0/test/kurtosis.js 0000664 0000000 0000000 00000000525 13102410767 0016500 0 ustar 00root root 0000000 0000000 var d3 = require("d3-array");
module.exports = function(numbers) {
var mean = d3.mean(numbers),
sum4 = 0,
sum2 = 0,
v,
i = -1,
n = numbers.length;
while (++i < n) {
v = numbers[i] - mean;
sum2 += v * v;
sum4 += v * v * v * v;
}
return (1 / n) * sum4 / Math.pow((1 / n) * sum2, 2) - 3;
};
d3-random-1.1.0/test/logNormal-test.js 0000664 0000000 0000000 00000004115 13102410767 0017523 0 ustar 00root root 0000000 0000000 var tape = require("tape"),
seedrandom = require("seedrandom"),
d3 = Object.assign({}, require("../"), require("d3-array"));
require("./inDelta");
tape.test("d3.randomLogNormal() returns random numbers with a log-mean of zero", function(test) {
var randomLogNormal = d3.randomLogNormal.source(seedrandom("a22ebc7c488a3a47"));
test.inDelta(d3.mean(d3.range(10000).map(randomLogNormal()), Math.log), 0, 0.05);
test.end();
});
tape.test("d3.randomLogNormal() returns random numbers with a log-standard deviation of one", function(test) {
var randomLogNormal = d3.randomLogNormal.source(seedrandom("06fd26b46c25607e"));
test.inDelta(d3.deviation(d3.range(10000).map(randomLogNormal()), Math.log), 1, 0.05);
test.end();
});
tape.test("d3.randomLogNormal(mu) returns random numbers with the specified log-mean", function(test) {
var randomLogNormal = d3.randomLogNormal.source(seedrandom("fffe77600db5c1ad"));
test.inDelta(d3.mean(d3.range(10000).map(randomLogNormal(42)), Math.log), 42, 0.05);
test.inDelta(d3.mean(d3.range(10000).map(randomLogNormal(-2)), Math.log), -2, 0.05);
test.end();
});
tape.test("d3.randomLogNormal(mu) returns random numbers with a log-standard deviation of 1", function(test) {
var randomLogNormal = d3.randomLogNormal.source(seedrandom("9caf2156de45315a"));
test.inDelta(d3.deviation(d3.range(10000).map(randomLogNormal(42)), Math.log), 1, 0.05);
test.inDelta(d3.deviation(d3.range(10000).map(randomLogNormal(-2)), Math.log), 1, 0.05);
test.end();
});
tape.test("d3.randomLogNormal(mu, sigma) returns random numbers with the specified log-mean and log-standard deviation", function(test) {
var randomLogNormal = d3.randomLogNormal.source(seedrandom("c0d761f591fb5e43"));
test.inDelta(d3.mean(d3.range(10000).map(randomLogNormal(42, 2)), Math.log), 42, 0.05);
test.inDelta(d3.mean(d3.range(10000).map(randomLogNormal(-2, 2)), Math.log), -2, 0.05);
test.inDelta(d3.deviation(d3.range(10000).map(randomLogNormal(42, 2)), Math.log), 2, 0.05);
test.inDelta(d3.deviation(d3.range(10000).map(randomLogNormal(-2, 2)), Math.log), 2, 0.05);
test.end();
});
d3-random-1.1.0/test/normal-test.js 0000664 0000000 0000000 00000003555 13102410767 0017070 0 ustar 00root root 0000000 0000000 var tape = require("tape"),
seedrandom = require("seedrandom"),
d3 = Object.assign({}, require("../"), require("d3-array"));
require("./inDelta");
tape.test("randomNormal() returns random numbers with a mean of zero", function(test) {
var randomNormal = d3.randomNormal.source(seedrandom("a22ebc7c488a3a47"));
test.inDelta(d3.mean(d3.range(10000).map(randomNormal())), 0, .05);
test.end();
});
tape.test("randomNormal() returns random numbers with a standard deviation of one", function(test) {
var randomNormal = d3.randomNormal.source(seedrandom("06fd26b46c25607e"));
test.inDelta(d3.deviation(d3.range(10000).map(randomNormal())), 1, .05);
test.end();
});
tape.test("randomNormal(mu) returns random numbers with the specified mean", function(test) {
var randomNormal = d3.randomNormal.source(seedrandom("fffe77600db5c1ad"));
test.inDelta(d3.mean(d3.range(10000).map(randomNormal(42))), 42, .05);
test.inDelta(d3.mean(d3.range(10000).map(randomNormal(-2))), -2, .05);
test.end();
});
tape.test("randomNormal(mu) returns random numbers with a standard deviation of 1", function(test) {
var randomNormal = d3.randomNormal.source(seedrandom("9caf2156de45315a"));
test.inDelta(d3.deviation(d3.range(10000).map(randomNormal(42))), 1, .05);
test.inDelta(d3.deviation(d3.range(10000).map(randomNormal(-2))), 1, .05);
test.end();
});
tape.test("randomNormal(mu, sigma) returns random numbers with the specified mean and standard deviation", function(test) {
var randomNormal = d3.randomNormal.source(seedrandom("c0d761f591fb5e43"));
test.inDelta(d3.mean(d3.range(10000).map(randomNormal(42, 2))), 42, .05);
test.inDelta(d3.mean(d3.range(10000).map(randomNormal(-2, 2))), -2, .05);
test.inDelta(d3.deviation(d3.range(10000).map(randomNormal(42, 2))), 2, .05);
test.inDelta(d3.deviation(d3.range(10000).map(randomNormal(-2, 2))), 2, .05);
test.end();
});
d3-random-1.1.0/test/skewness.js 0000664 0000000 0000000 00000000527 13102410767 0016461 0 ustar 00root root 0000000 0000000 var d3 = require("d3-array");
module.exports = function(numbers) {
var mean = d3.mean(numbers),
sum3 = 0,
sum2 = 0,
v,
i = -1,
n = numbers.length;
while (++i < n) {
v = numbers[i] - mean;
sum2 += v * v;
sum3 += v * v * v;
}
return (1 / n) * sum3 / Math.pow((1 / (n - 1)) * sum2, 3 / 2);
};
d3-random-1.1.0/test/uniform-test.js 0000664 0000000 0000000 00000003623 13102410767 0017253 0 ustar 00root root 0000000 0000000 var tape = require("tape"),
seedrandom = require("seedrandom"),
d3 = Object.assign({}, require("../"), require("d3-array"));
require("./inDelta");
tape.test("randomUniform() returns random numbers with a mean of 0.5", function(test) {
var randomUniform = d3.randomUniform.source(seedrandom("d7bcbccaa96bba8c"));
test.inDelta(d3.mean(d3.range(10000).map(randomUniform())), 0.5, .05);
test.end();
});
tape.test("randomUniform() returns random numbers within the range [0,1)", function(test) {
var randomUniform = d3.randomUniform.source(seedrandom("f232de9b208a7137"));
test.ok(d3.min(d3.range(10000).map(randomUniform())) >= 0);
test.ok(d3.min(d3.range(10000).map(randomUniform())) < 1);
test.end();
});
tape.test("randomUniform(max) returns random numbers with a mean of max / 2", function(test) {
var randomUniform = d3.randomUniform.source(seedrandom("c52f4e6cd112aada"));
test.inDelta(d3.mean(d3.range(10000).map(randomUniform(42))), 21, .5);
test.end();
});
tape.test("randomUniform(max) returns random numbers within the range [0,max)", function(test) {
var randomUniform = d3.randomUniform.source(seedrandom("8f2959eba39debfd"));
test.ok(d3.min(d3.range(10000).map(randomUniform(42))) >= 0);
test.ok(d3.min(d3.range(10000).map(randomUniform(42))) < 42);
test.end();
});
tape.test("randomUniform(min, max) returns random numbers with a mean of (min + max) / 2", function(test) {
var randomUniform = d3.randomUniform.source(seedrandom("5ea383210c9bdd21"));
test.inDelta(d3.mean(d3.range(10000).map(randomUniform(10, 42))), 26, .5);
test.end();
});
tape.test("randomUniform(min, max) returns random numbers within the range [min,max)", function(test) {
var randomUniform = d3.randomUniform.source(seedrandom("88f461e6b7454981"));
test.ok(d3.min(d3.range(10000).map(randomUniform(10, 42))) >= 10);
test.ok(d3.min(d3.range(10000).map(randomUniform(10, 42))) < 42);
test.end();
});