• Stars
    star
    136
  • Rank 258,069 (Top 6 %)
  • Language
    JavaScript
  • License
    ISC License
  • Created almost 9 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Generate random numbers from various distributions.

d3-random

Generate random numbers from various distributions.

See the d3-random collection on Observable for examples.

Installing

If you use npm, npm install d3-random. You can also download the latest release on GitHub. For vanilla HTML in modern browsers, import d3-random from Skypack:

<script type="module">

import {randomUniform} from "https://cdn.skypack.dev/d3-random@3";

const random = randomUniform(1, 10);

</script>

For legacy environments, you can load d3-random’s UMD bundle from an npm-based CDN such as jsDelivr; a d3 global is exported:

<script src="https://cdn.jsdelivr.net/npm/d3-random@3"></script>
<script>

const random = d3.randomUniform(1, 10);

</script>

API Reference

# d3.randomUniform([min, ][max]) · Source, Examples

Returns a function for generating random numbers with a uniform distribution. 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:

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.

# d3.randomInt([min, ][max]) · Source, Examples

Returns a function for generating random integers with a uniform distribution. 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:

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.

# d3.randomNormal([mu][, sigma]) · Source, Examples

Returns a function for generating random numbers with a normal (Gaussian) 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]) · Source, Examples

Returns a function for generating random numbers with a 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) · Source, Examples

Returns a function for generating random numbers with a Bates distribution with n independent variables. The case of fractional n is handled as with d3.randomIrwinHall, and d3.randomBates(0) is equivalent to d3.randomUniform().

# d3.randomIrwinHall(n) · Source, Examples

Returns a function for generating random numbers with an Irwin–Hall distribution with n independent variables. If the fractional part of n is non-zero, this is treated as adding d3.randomUniform() times that fractional part to the integral part.

# d3.randomExponential(lambda) · Source, Examples

Returns a function for generating random numbers with an exponential distribution with the rate lambda; equivalent to time between events in a Poisson 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.

# d3.randomPareto(alpha) · Source, Examples

Returns a function for generating random numbers with a Pareto distribution with the shape alpha. The value alpha must be a positive value.

# d3.randomBernoulli(p) · Source, Examples

Returns a function for generating either 1 or 0 according to a Bernoulli 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].

# d3.randomGeometric(p) · Source, Examples

Returns a function for generating numbers with a geometric distribution with success probability p. The value p is in the range [0, 1].

# d3.randomBinomial(n, p) · Source, Examples

Returns a function for generating random numbers with a 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].

# d3.randomGamma(k, [theta]) · Source, Examples

Returns a function for generating random numbers with a gamma distribution with k the shape parameter and theta the scale parameter. The value k must be a positive value; if theta is not specified, it defaults to 1.

# d3.randomBeta(alpha, beta) · Source, Examples

Returns a function for generating random numbers with a beta distribution with alpha and beta shape parameters, which must both be positive.

# d3.randomWeibull(k, [a], [b]) · Source, Examples

Returns a function for generating random numbers with one of the generalized extreme value distributions, depending on k:

In all three cases, a is the location parameter and b is the scale parameter. If a is not specified, it defaults to 0; if b is not specified, it defaults to 1.

# d3.randomCauchy([a], [b]) · Source, Examples

Returns a function for generating random numbers with a Cauchy distribution. a and b have the same meanings and default values as in d3.randomWeibull.

# d3.randomLogistic([a], [b]) · Source, Examples

Returns a function for generating random numbers with a logistic distribution. a and b have the same meanings and default values as in d3.randomWeibull.

# d3.randomPoisson(lambda) · Source, Examples

Returns a function for generating random numbers with a Poisson distribution with mean lambda.

# random.source(source) · Examples

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:

import {randomLcg, randomNormal} from "d3-random";

const seed = 0.44871573888282423; // any number in [0, 1)
const random = randomNormal.source(randomLcg(seed))(0, 1);

random(); // -0.6253955998897069

# d3.randomLcg([seed]) · Source, Examples

Returns a linear congruential generator; this function can be called repeatedly to obtain pseudorandom values well-distributed on the interval [0,1) and with a long period (up to 1 billion numbers), similar to Math.random. A seed can be specified as a real number in the interval [0,1) or as any integer. In the latter case, only the lower 32 bits are considered. Two generators instanced with the same seed generate the same sequence, allowing to create reproducible pseudo-random experiments. If the seed is not specified, one is chosen using Math.random.

More Repositories

1

d3

Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript
106,311
star
2

d3-shape

Graphical primitives for visualization, such as lines and areas.
JavaScript
2,458
star
3

d3-plugins

[DEPRECATED] A repository for sharing D3.js V3 plugins.
JavaScript
1,808
star
4

d3-force

Force-directed graph layout using velocity Verlet integration.
JavaScript
1,702
star
5

d3-scale

Encodings that map abstract data to visual representation.
JavaScript
1,567
star
6

d3-queue

Evaluate asynchronous tasks with configurable concurrency.
JavaScript
1,411
star
7

d3-hierarchy

2D layout algorithms for visualizing hierarchical data.
JavaScript
1,064
star
8

d3-geo-projection

Extended geographic projections for d3-geo.
JavaScript
1,058
star
9

d3-geo

Geographic projections, spherical shapes and spherical trigonometry.
JavaScript
988
star
10

d3-scale-chromatic

Sequential, diverging and categorical color scales.
JavaScript
787
star
11

d3-sankey

Visualize flow between nodes in a directed acyclic network.
JavaScript
763
star
12

d3-format

Format numbers for human consumption.
JavaScript
611
star
13

d3-ease

Easing functions for smooth animation.
JavaScript
604
star
14

d3-delaunay

Compute the Voronoi diagram of a set of two-dimensional points.
JavaScript
588
star
15

d3-selection

Transform the DOM by selecting elements and joining to data.
JavaScript
547
star
16

d3-zoom

Pan and zoom SVG, HTML or Canvas using mouse or touch input.
JavaScript
495
star
17

d3-contour

Compute contour polygons using marching squares.
JavaScript
487
star
18

d3-interpolate

Interpolate numbers, colors, strings, arrays, objects, whatever!
JavaScript
482
star
19

d3-array

Array manipulation, ordering, searching, summarizing, etc.
JavaScript
452
star
20

d3-dsv

A parser and formatter for delimiter-separated values, such as CSV and TSV.
JavaScript
416
star
21

d3-color

Color spaces! RGB, HSL, Cubehelix, CIELAB, and more.
JavaScript
389
star
22

d3-drag

Drag and drop SVG, HTML or Canvas using mouse or touch input.
JavaScript
328
star
23

d3-time-format

Parse and format times, inspired by strptime and strftime.
JavaScript
324
star
24

d3-voronoi

Compute the Voronoi diagram of a set of two-dimensional points.
JavaScript
250
star
25

d3-hexbin

Group two-dimensional points into hexagonal bins.
JavaScript
231
star
26

d3-time

A calculator for humanity’s peculiar conventions of time.
JavaScript
227
star
27

d3-quadtree

Two-dimensional recursive spatial subdivision.
JavaScript
225
star
28

d3-transition

Animated transitions for D3 selections.
JavaScript
219
star
29

d3-fetch

Convenient parsing for Fetch.
JavaScript
215
star
30

d3-axis

Human-readable reference marks for scales.
JavaScript
204
star
31

d3.github.com

The D3 website.
JavaScript
195
star
32

d3-path

Serialize Canvas path commands to SVG.
JavaScript
192
star
33

d3-timer

An efficient queue for managing thousands of concurrent animations.
JavaScript
159
star
34

d3-brush

Select a one- or two-dimensional region using the mouse or touch.
JavaScript
154
star
35

d3-3.x-api-reference

An archive of the D3 3.x API Reference.
153
star
36

d3-chord

Visualizations relationships or network flow with a circular layout.
JavaScript
122
star
37

d3-tile

Compute the quadtree tiles to display in a rectangular viewport.
JavaScript
120
star
38

d3-collection

Handy data structures for elements keyed by string.
JavaScript
111
star
39

d3-request

A convenient alternative to XMLHttpRequest.
JavaScript
109
star
40

d3-geo-polygon

Clipping and geometric operations for spherical polygons.
JavaScript
102
star
41

d3-polygon

Geometric operations for two-dimensional polygons.
JavaScript
97
star
42

d3-require

A minimal, promise-based implementation to require asynchronous module definitions.
JavaScript
78
star
43

d3-selection-multi

Multi-value syntax for d3-selection and d3-transition.
JavaScript
75
star
44

d3-dispatch

Register named callbacks and call them with arguments.
JavaScript
75
star
45

versor

a home for Mike Bostock's versor.js
JavaScript
34
star
46

d3-bundler

DEPRECATED; use rollup/rollup.
JavaScript
34
star
47

d3-hsv

The HSV (Hue, Saturation, Value) color space.
JavaScript
26
star
48

d3-logo

D3 brand assets.
23
star
49

d3-cam16

A d3 implementation of the CIECAM16 color appearance model.
JavaScript
22
star
50

d3-hcg

The HCG (Hue, Chroma, Grayness) color space derived from the Munsell color system.
JavaScript
20
star
51

d3-scripts

Common scripts for D3 modules.
JavaScript
15
star
52

d3-hull

DEPRECATED; see d3-polygon’s hull function.
JavaScript
14
star
53

blur-benchmark

temporary benchmark for d3.blur implementations
JavaScript
2
star