d3-random
Advanced tools
Comparing version 1.1.2 to 2.0.0
@@ -1,11 +0,9 @@ | ||
// https://d3js.org/d3-random/ v1.1.2 Copyright 2018 Mike Bostock | ||
// https://d3js.org/d3-random/ v2.0.0 Copyright 2019 Mike Bostock | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(factory((global.d3 = global.d3 || {}))); | ||
}(this, (function (exports) { 'use strict'; | ||
(global = global || self, factory(global.d3 = global.d3 || {})); | ||
}(this, function (exports) { 'use strict'; | ||
function defaultSource() { | ||
return Math.random(); | ||
} | ||
var defaultSource = Math.random; | ||
@@ -28,2 +26,17 @@ var uniform = (function sourceRandomUniform(source) { | ||
var int = (function sourceRandomInt(source) { | ||
function randomInt(min, max) { | ||
if (arguments.length < 2) max = min, min = 0; | ||
min = Math.floor(min); | ||
max = Math.floor(max) - min; | ||
return function() { | ||
return Math.floor(source() * max + min); | ||
}; | ||
} | ||
randomInt.source = sourceRandomInt; | ||
return randomInt; | ||
})(defaultSource); | ||
var normal = (function sourceRandomNormal(source) { | ||
@@ -107,11 +120,71 @@ function randomNormal(mu, sigma) { | ||
exports.randomUniform = uniform; | ||
exports.randomNormal = normal; | ||
exports.randomLogNormal = logNormal; | ||
var pareto = (function sourceRandomPareto(source) { | ||
function randomPareto(alpha) { | ||
if ((alpha = +alpha) < 0) throw new RangeError("invalid alpha"); | ||
alpha = 1 / -alpha; | ||
return function() { | ||
return Math.pow(1 - source(), alpha); | ||
}; | ||
} | ||
randomPareto.source = sourceRandomPareto; | ||
return randomPareto; | ||
})(defaultSource); | ||
var bernoulli = (function sourceRandomBernoulli(source) { | ||
function randomBernoulli(p) { | ||
if ((p = +p) < 0 || p > 1) throw new RangeError("invalid p"); | ||
return function() { | ||
return Math.floor(source() + p); | ||
}; | ||
} | ||
randomBernoulli.source = sourceRandomBernoulli; | ||
return randomBernoulli; | ||
})(defaultSource); | ||
var geometric = (function sourceRandomGeometric(source) { | ||
function randomGeometric(p) { | ||
if ((p = 1 - p) < 0 || p >= 1) throw new RangeError("invalid p"); | ||
return function() { | ||
return 1 + Math.floor(Math.log(source()) / Math.log(p)); | ||
}; | ||
} | ||
randomGeometric.source = sourceRandomGeometric; | ||
return randomGeometric; | ||
})(defaultSource); | ||
var binomial = (function sourceRandomBinomial(source) { | ||
function randomBinomial(n, p) { | ||
n = +n, p = +p; | ||
return function() { | ||
var i = -1, x = 0; | ||
while (++i < n) x += source() < p; | ||
return x; | ||
}; | ||
} | ||
randomBinomial.source = sourceRandomBinomial; | ||
return randomBinomial; | ||
})(defaultSource); | ||
exports.randomBates = bates; | ||
exports.randomBernoulli = bernoulli; | ||
exports.randomBinomial = binomial; | ||
exports.randomExponential = exponential; | ||
exports.randomGeometric = geometric; | ||
exports.randomInt = int; | ||
exports.randomIrwinHall = irwinHall; | ||
exports.randomExponential = exponential; | ||
exports.randomLogNormal = logNormal; | ||
exports.randomNormal = normal; | ||
exports.randomPareto = pareto; | ||
exports.randomUniform = uniform; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
}))); | ||
})); |
@@ -1,2 +0,2 @@ | ||
// https://d3js.org/d3-random/ v1.1.2 Copyright 2018 Mike Bostock | ||
!function(n,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r(n.d3=n.d3||{})}(this,function(n){"use strict";function r(){return Math.random()}var t=function n(r){function t(n,t){return n=null==n?0:+n,t=null==t?1:+t,1===arguments.length?(t=n,n=0):t-=n,function(){return r()*t+n}}return t.source=n,t}(r),u=function n(r){function t(n,t){var u,e;return n=null==n?0:+n,t=null==t?1:+t,function(){var o;if(null!=u)o=u,u=null;else do{u=2*r()-1,o=2*r()-1,e=u*u+o*o}while(!e||e>1);return n+t*o*Math.sqrt(-2*Math.log(e)/e)}}return t.source=n,t}(r),e=function n(r){function t(){var n=u.source(r).apply(this,arguments);return function(){return Math.exp(n())}}return t.source=n,t}(r),o=function n(r){function t(n){return function(){for(var t=0,u=0;u<n;++u)t+=r();return t}}return t.source=n,t}(r),i=function n(r){function t(n){var t=o.source(r)(n);return function(){return t()/n}}return t.source=n,t}(r),c=function n(r){function t(n){return function(){return-Math.log(1-r())/n}}return t.source=n,t}(r);n.randomUniform=t,n.randomNormal=u,n.randomLogNormal=e,n.randomBates=i,n.randomIrwinHall=o,n.randomExponential=c,Object.defineProperty(n,"__esModule",{value:!0})}); | ||
// https://d3js.org/d3-random/ v2.0.0 Copyright 2019 Mike Bostock | ||
!function(n,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r((n=n||self).d3=n.d3||{})}(this,function(n){"use strict";var r=Math.random,t=function n(r){function t(n,t){return n=null==n?0:+n,t=null==t?1:+t,1===arguments.length?(t=n,n=0):t-=n,function(){return r()*t+n}}return t.source=n,t}(r),o=function n(r){function t(n,t){return arguments.length<2&&(t=n,n=0),n=Math.floor(n),t=Math.floor(t)-n,function(){return Math.floor(r()*t+n)}}return t.source=n,t}(r),u=function n(r){function t(n,t){var o,u;return n=null==n?0:+n,t=null==t?1:+t,function(){var e;if(null!=o)e=o,o=null;else do{o=2*r()-1,e=2*r()-1,u=o*o+e*e}while(!u||u>1);return n+t*e*Math.sqrt(-2*Math.log(u)/u)}}return t.source=n,t}(r),e=function n(r){function t(){var n=u.source(r).apply(this,arguments);return function(){return Math.exp(n())}}return t.source=n,t}(r),i=function n(r){function t(n){return function(){for(var t=0,o=0;o<n;++o)t+=r();return t}}return t.source=n,t}(r),f=function n(r){function t(n){var t=i.source(r)(n);return function(){return t()/n}}return t.source=n,t}(r),c=function n(r){function t(n){return function(){return-Math.log(1-r())/n}}return t.source=n,t}(r),a=function n(r){function t(n){if((n=+n)<0)throw new RangeError("invalid alpha");return n=1/-n,function(){return Math.pow(1-r(),n)}}return t.source=n,t}(r),l=function n(r){function t(n){if((n=+n)<0||n>1)throw new RangeError("invalid p");return function(){return Math.floor(r()+n)}}return t.source=n,t}(r),d=function n(r){function t(n){if((n=1-n)<0||n>=1)throw new RangeError("invalid p");return function(){return 1+Math.floor(Math.log(r())/Math.log(n))}}return t.source=n,t}(r),s=function n(r){function t(n,t){return n=+n,t=+t,function(){for(var o=-1,u=0;++o<n;)u+=r()<t;return u}}return t.source=n,t}(r);n.randomBates=f,n.randomBernoulli=l,n.randomBinomial=s,n.randomExponential=c,n.randomGeometric=d,n.randomInt=o,n.randomIrwinHall=i,n.randomLogNormal=e,n.randomNormal=u,n.randomPareto=a,n.randomUniform=t,Object.defineProperty(n,"__esModule",{value:!0})}); |
{ | ||
"name": "d3-random", | ||
"version": "1.1.2", | ||
"version": "2.0.0", | ||
"description": "Generate random numbers from various distributions.", | ||
@@ -25,2 +25,6 @@ "keywords": [ | ||
}, | ||
"files": [ | ||
"dist/**/*.js", | ||
"src/**/*.js" | ||
], | ||
"scripts": { | ||
@@ -33,6 +37,7 @@ "pretest": "rollup -c", | ||
"devDependencies": { | ||
"d3-array": "1", | ||
"eslint": "5", | ||
"rollup": "0.64", | ||
"rollup-plugin-terser": "1", | ||
"d3-array": "1 - 2", | ||
"eslint": "6", | ||
"jsdom": "15", | ||
"rollup": "1", | ||
"rollup-plugin-terser": "5", | ||
"seedrandom": "2", | ||
@@ -39,0 +44,0 @@ "tape": "4" |
@@ -5,8 +5,10 @@ # d3-random | ||
See the [d3-random collection on Observable](https://observablehq.com/collection/@d3/d3-random) for examples. | ||
## 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: | ||
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 as a [standalone library](https://d3js.org/d3-random.v2.min.js) or as part of [D3](https://github.com/d3/d3). ES modules, AMD, CommonJS, and vanilla environments are supported. In vanilla, a `d3` global is exported: | ||
```html | ||
<script src="https://d3js.org/d3-random.v1.min.js"></script> | ||
<script src="https://d3js.org/d3-random.v2.min.js"></script> | ||
<script> | ||
@@ -19,4 +21,2 @@ | ||
[Try d3-random in your browser.](https://runkit.com/npm/d3-random) | ||
## API Reference | ||
@@ -26,3 +26,3 @@ | ||
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: | ||
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* (inclusive), and the maximum is *max* (exclusive). If *min* is not specified, it defaults to 0; if *max* is not specified, it defaults to 1. For example: | ||
@@ -34,4 +34,11 @@ ```js | ||
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="randomInt" href="#randomInt">#</a> d3.<b>randomInt</b>([<i>min</i>, ][<i>max</i>]) [<>](https://github.com/d3/d3-random/blob/master/src/int.js "Source") | ||
Returns a function for generating random integers with a [uniform distribution](https://en.wikipedia.org/wiki/Uniform_distribution_\(continuous\)). The minimum allowed value of a returned number is ⌊*min*⌋ (inclusive), and the maximum is ⌊*max* - 1⌋ (inclusive). If *min* is not specified, it defaults to 0. For example: | ||
```js | ||
d3.randomInt(6)(); // Returns an integer greater than or equal to 0 and less than 6. | ||
d3.randomInt(1, 5)(); // Returns an integer greater than or equal to 1 and less than 5. | ||
``` | ||
<a name="randomNormal" href="#randomNormal">#</a> d3.<b>randomNormal</b>([<i>mu</i>][, <i>sigma</i>]) [<>](https://github.com/d3/d3-random/blob/master/src/normal.js "Source") | ||
@@ -57,2 +64,18 @@ | ||
<a name="randomPareto" href="#randomPareto">#</a> d3.<b>randomPareto</b>(<i>alpha</i>) [<>](https://github.com/d3/d3-random/blob/master/src/pareto.js "Source") | ||
Returns a function for generating random numbers with an [Pareto distribution](https://en.wikipedia.org/wiki/Pareto_distribution) with the shape *alpha*. The value *alpha* must be a positive value. | ||
<a name="randomBernoulli" href="#randomBernoulli">#</a> d3.<b>randomBernoulli</b>(<i>p</i>) [<>](https://github.com/d3/d3-random/blob/master/src/bernoulli.js "Source") | ||
Returns a function for generating either 1 or 0 according to a [Bernoulli distribution](https://en.wikipedia.org/wiki/Binomial_distribution) with 1 being returned with success probability *p* and 0 with failure probability *q* = 1 - *p*. The value *p* is in the range [0, 1]. | ||
<a name="randomGeometric" href="#randomGeometric">#</a> d3.<b>randomGeometric</b>(<i>p</i>) [<>](https://github.com/d3/d3-random/blob/master/src/geometric.js "Source") | ||
Returns a function for generating numbers with a [geometric distribution](https://en.wikipedia.org/wiki/Geometric_distribution) with success probability *p*. The value *p* is in the range (0, 1]. | ||
<a name="randomBinomial" href="#randomBinomial">#</a> d3.<b>randomBinomial</b>(<i>n</i>, <i>p</i>) [<>](https://github.com/d3/d3-random/blob/master/src/binomial.js "Source") | ||
Returns a function for generating random numbers with a [binomial distribution](https://en.wikipedia.org/wiki/Binomial_distribution) with *n* the number of trials and *p* the probability of success in each trial. The value *n* is greater or equal to 0, and the value *p* is in the range [0, 1]. | ||
<a name="random_source" href="#random_source">#</a> <i>random</i>.<b>source</b>(<i>source</i>) | ||
@@ -59,0 +82,0 @@ |
@@ -1,3 +0,3 @@ | ||
import defaultSource from "./defaultSource"; | ||
import irwinHall from "./irwinHall"; | ||
import defaultSource from "./defaultSource.js"; | ||
import irwinHall from "./irwinHall.js"; | ||
@@ -4,0 +4,0 @@ export default (function sourceRandomBates(source) { |
@@ -1,3 +0,1 @@ | ||
export default function() { | ||
return Math.random(); | ||
} | ||
export default Math.random; |
@@ -1,2 +0,2 @@ | ||
import defaultSource from "./defaultSource"; | ||
import defaultSource from "./defaultSource.js"; | ||
@@ -3,0 +3,0 @@ export default (function sourceRandomExponential(source) { |
@@ -1,6 +0,11 @@ | ||
export {default as randomUniform} from "./uniform"; | ||
export {default as randomNormal} from "./normal"; | ||
export {default as randomLogNormal} from "./logNormal"; | ||
export {default as randomBates} from "./bates"; | ||
export {default as randomIrwinHall} from "./irwinHall"; | ||
export {default as randomExponential} from "./exponential"; | ||
export {default as randomUniform} from "./uniform.js"; | ||
export {default as randomInt} from "./int.js"; | ||
export {default as randomNormal} from "./normal.js"; | ||
export {default as randomLogNormal} from "./logNormal.js"; | ||
export {default as randomBates} from "./bates.js"; | ||
export {default as randomIrwinHall} from "./irwinHall.js"; | ||
export {default as randomExponential} from "./exponential.js"; | ||
export {default as randomPareto} from "./pareto.js"; | ||
export {default as randomBernoulli} from "./bernoulli.js"; | ||
export {default as randomGeometric} from "./geometric.js"; | ||
export {default as randomBinomial} from "./binomial.js"; |
@@ -1,2 +0,2 @@ | ||
import defaultSource from "./defaultSource"; | ||
import defaultSource from "./defaultSource.js"; | ||
@@ -3,0 +3,0 @@ export default (function sourceRandomIrwinHall(source) { |
@@ -1,3 +0,3 @@ | ||
import defaultSource from "./defaultSource"; | ||
import normal from "./normal"; | ||
import defaultSource from "./defaultSource.js"; | ||
import normal from "./normal.js"; | ||
@@ -4,0 +4,0 @@ export default (function sourceRandomLogNormal(source) { |
@@ -1,2 +0,2 @@ | ||
import defaultSource from "./defaultSource"; | ||
import defaultSource from "./defaultSource.js"; | ||
@@ -3,0 +3,0 @@ export default (function sourceRandomNormal(source) { |
@@ -1,2 +0,2 @@ | ||
import defaultSource from "./defaultSource"; | ||
import defaultSource from "./defaultSource.js"; | ||
@@ -3,0 +3,0 @@ export default (function sourceRandomUniform(source) { |
18
310
87
22006
7