d3-random # d3-random

## Generate random numbers from various distributions.

### 2.0.0  •  Updated 8 months ago  •  by d3  •  BSD 3-Clause "New" or "Revised" License

Generate random numbers from various distributions.

See the d3-random collection on Observable for examples.

## Installing

If you use NPM, `npm install d3-random`. Otherwise, download the latest release. You can also load directly as a standalone library or as part of D3. ES modules, AMD, CommonJS, and vanilla environments are supported. In vanilla, a `d3` global is exported:

``````<script src="https://d3js.org/d3-random.v2.min.js"></script>
<script>

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

</script>
``````

## API Reference

# d3.randomUniform([min, ][max]) <>

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]) <>

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]) <>

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]) <>

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) <>

Returns a function for generating random numbers with a Bates distribution with n independent variables.

# d3.randomIrwinHall(n) <>

Returns a function for generating random numbers with an Irwin–Hall distribution with n independent variables.

# d3.randomExponential(lambda) <>

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) <>

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

# d3.randomBernoulli(p) <>

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) <>

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) <>

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].

# 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:

``````var d3 = require("d3-random"),
seedrandom = require("seedrandom"),
random = d3.randomNormal.source(seedrandom("a22ebc7c488a3a47"))(0, 1);

random(); // 0.9744193494813501
``````

531.6K
87

## Development

Last ver 8 months ago
Created 6 years ago
Last commit 4 months ago
17 days between commits

## Technology

Node version: 10.14.1
22K unpacked

## Compliance

BSD 3-Clause "New" or "Revised" License
OSI Approved
0 vulnerabilities

## Contributors

8 contributors
Mike Bostock
Maintainer, 73 commits, 7 merges
Works at observablehq
Taylor Baldwin
13 commits, 1 PRs
Works at uber
Noah
3 commits, 1 PRs
1 commits, 1 PRs
Works at Mango-information-systems
Harrison Shoff
1 commits, 1 PRs
Works at Airbnb
Alex Van Camp
1 commits, 1 PRs
Works at GamesDoneQuick

d3
d3-module
random
rng