
Security News
Create React App Officially Deprecated Amid React 19 Compatibility Issues
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
The d3-random package is part of the D3 (Data-Driven Documents) JavaScript library, which is primarily used for generating random numbers with specific statistical properties. This package is useful for simulations, animations, and generating sample data for testing or visualization purposes.
Uniform Distribution
Generates a random number following a uniform distribution. In the provided code, random numbers between 1 and 5 are generated.
const d3 = require('d3-random');
const randomUniform = d3.randomUniform(1, 5);
console.log(randomUniform());
Normal Distribution
Generates a random number following a normal (Gaussian) distribution. The code example creates a standard normal distribution with a mean of 0 and a standard deviation of 1.
const d3 = require('d3-random');
const randomNormal = d3.randomNormal(0, 1);
console.log(randomNormal());
Log-Normal Distribution
Generates a random number following a log-normal distribution. The code example uses a mean of 0 and a standard deviation of 1 for the underlying normal distribution.
const d3 = require('d3-random');
const randomLogNormal = d3.randomLogNormal(0, 1);
console.log(randomLogNormal());
Bates Distribution
Generates a random number following a Bates distribution, which is useful for simulating the average of samples from a uniform distribution. The code example averages 10 samples.
const d3 = require('d3-random');
const randomBates = d3.randomBates(10);
console.log(randomBates());
Irwin-Hall Distribution
Generates a random number following an Irwin-Hall distribution. The code example sums 10 samples from a uniform distribution.
const d3 = require('d3-random');
const randomIrwinHall = d3.randomIrwinHall(10);
console.log(randomIrwinHall());
random-js is a mathematically correct random number generator library for JavaScript. It offers a variety of distributions and utilities similar to d3-random but with a broader focus on overall randomness utilities, including seeding capabilities which d3-random lacks.
Chance is a minimalist generator of random strings, numbers, etc. to help reduce some monotony particularly while writing automated tests or anywhere else you need anything random. It provides more types of random generators compared to d3-random, such as random names, addresses, and more complex data types.
Generate random numbers from various distributions.
See the d3-random collection on Observable for examples.
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>
# 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, randomNumber} 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.
FAQs
Generate random numbers from various distributions.
The npm package d3-random receives a total of 1,187,595 weekly downloads. As such, d3-random popularity was classified as popular.
We found that d3-random demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.