Generate random numbers from various statistical distributions for data visualization and scientific computing.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Specialized statistical distributions for scientific computing, advanced simulations, and specific modeling requirements. These distributions are commonly used in statistical analysis, machine learning, and specialized scientific applications.
Generates gamma distributed random numbers, useful for modeling waiting times and other skewed positive distributions.
/**
* Returns a function for generating gamma distribution random numbers
* @param k - Shape parameter (must be positive)
* @param theta - Scale parameter, defaults to 1
* @returns Generator function producing gamma distributed numbers
* @throws RangeError if k < 0
*/
function randomGamma(k: number, theta?: number): () => number;Usage Examples:
import { randomGamma } from "d3-random";
// Service time distribution (shape=2, scale=3)
const serviceTime = randomGamma(2, 3);
console.log(serviceTime().toFixed(2)); // e.g., 4.23
// Exponential distribution (special case: k=1)
const exponential = randomGamma(1, 2);
console.log(exponential().toFixed(2)); // e.g., 1.87
try {
const invalid = randomGamma(-1); // Throws RangeError
} catch (error) {
console.error("Invalid gamma parameter");
}Generates beta distributed random numbers, commonly used for modeling proportions and probabilities.
/**
* Returns a function for generating beta distribution random numbers
* @param alpha - First shape parameter (must be positive)
* @param beta - Second shape parameter (must be positive)
* @returns Generator function producing numbers in range [0, 1]
*/
function randomBeta(alpha: number, beta: number): () => number;Usage Examples:
import { randomBeta } from "d3-random";
// Uniform distribution (special case: alpha=1, beta=1)
const uniform = randomBeta(1, 1);
console.log(uniform().toFixed(3)); // e.g., 0.743
// Biased toward 0 (alpha=0.5, beta=2)
const leftSkewed = randomBeta(0.5, 2);
console.log(leftSkewed().toFixed(3)); // e.g., 0.123
// Bell-shaped distribution (alpha=5, beta=5)
const bellShaped = randomBeta(5, 5);
console.log(bellShaped().toFixed(3)); // e.g., 0.567Generates numbers from the generalized extreme value distribution family, including Weibull, Gumbel, and Fréchet distributions.
/**
* Returns a function for generating generalized extreme value distribution random numbers
* @param k - Shape parameter (positive=Weibull, zero=Gumbel, negative=Fréchet)
* @param a - Location parameter, defaults to 0
* @param b - Scale parameter, defaults to 1
* @returns Generator function producing distributed numbers
*/
function randomWeibull(k: number, a?: number, b?: number): () => number;Usage Examples:
import { randomWeibull } from "d3-random";
// Weibull distribution (k > 0) - reliability analysis
const reliability = randomWeibull(2, 0, 1000);
console.log(Math.round(reliability())); // e.g., 823 hours
// Gumbel distribution (k = 0) - extreme values
const extremeValue = randomWeibull(0, 10, 2);
console.log(extremeValue().toFixed(2)); // e.g., 12.34
// Fréchet distribution (k < 0) - heavy-tailed
const heavyTailed = randomWeibull(-0.5, 0, 1);
console.log(heavyTailed().toFixed(2)); // e.g., 3.45Generates Cauchy distributed random numbers, a heavy-tailed distribution with undefined mean and variance.
/**
* Returns a function for generating Cauchy distribution random numbers
* @param a - Location parameter, defaults to 0
* @param b - Scale parameter, defaults to 1
* @returns Generator function producing Cauchy distributed numbers
*/
function randomCauchy(a?: number, b?: number): () => number;Usage Examples:
import { randomCauchy } from "d3-random";
// Standard Cauchy distribution
const standard = randomCauchy();
console.log(standard().toFixed(2)); // e.g., -2.34 (can be very large)
// Shifted and scaled Cauchy
const shifted = randomCauchy(100, 10);
console.log(shifted().toFixed(2)); // e.g., 87.65Generates logistically distributed random numbers, similar to normal but with heavier tails.
/**
* Returns a function for generating logistic distribution random numbers
* @param a - Location parameter, defaults to 0
* @param b - Scale parameter, defaults to 1
* @returns Generator function producing logistically distributed numbers
*/
function randomLogistic(a?: number, b?: number): () => number;Usage Examples:
import { randomLogistic } from "d3-random";
// Standard logistic distribution
const standard = randomLogistic();
console.log(standard().toFixed(3)); // e.g., 1.234
// Logistic regression-style parameters
const logReg = randomLogistic(0, 0.5);
console.log(logReg().toFixed(3)); // e.g., -0.876Generates Poisson distributed random numbers, used for modeling count data and rare events.
/**
* Returns a function for generating Poisson distribution random numbers
* @param lambda - Mean parameter (average number of events)
* @returns Generator function producing non-negative integers
*/
function randomPoisson(lambda: number): () => number;Usage Examples:
import { randomPoisson } from "d3-random";
// Number of customers per hour (average 5)
const customers = randomPoisson(5);
console.log(customers()); // e.g., 7
// Network packet arrivals (average 100 per second)
const packets = randomPoisson(100);
console.log(packets()); // e.g., 103
// Rare events (average 0.1 per day)
const rareEvents = randomPoisson(0.1);
console.log(rareEvents()); // Usually 0, occasionally 1 or moreGenerates Pareto distributed random numbers, commonly used for modeling wealth distribution and other power-law phenomena.
/**
* Returns a function for generating Pareto distribution random numbers
* @param alpha - Shape parameter (must be positive)
* @returns Generator function producing numbers ≥ 1
*/
function randomPareto(alpha: number): () => number;Usage Examples:
import { randomPareto } from "d3-random";
// Income distribution (80/20 rule with alpha=1.16)
const income = randomPareto(1.16);
console.log(income().toFixed(2)); // e.g., 2.34
// City size distribution
const citySize = randomPareto(2);
console.log(citySize().toFixed(2)); // e.g., 1.87Generates Bernoulli distributed random numbers (0 or 1) with specified success probability.
/**
* Returns a function for generating Bernoulli distribution random numbers
* @param p - Success probability, must be in range [0, 1]
* @returns Generator function producing 0 or 1
*/
function randomBernoulli(p: number): () => number;Usage Examples:
import { randomBernoulli } from "d3-random";
// Fair coin flip (50% probability)
const coinFlip = randomBernoulli(0.5);
console.log(coinFlip()); // 0 or 1
// Biased coin (70% chance of success)
const biasedCoin = randomBernoulli(0.7);
console.log(biasedCoin()); // 1 more often than 0
// Rare event (1% probability)
const rareEvent = randomBernoulli(0.01);
console.log(rareEvent()); // Usually 0, rarely 1Generates geometrically distributed random numbers, modeling the number of trials until the first success.
/**
* Returns a function for generating geometric distribution random numbers
* @param p - Success probability per trial, must be in range [0, 1]
* @returns Generator function producing positive integers
*/
function randomGeometric(p: number): () => number;Usage Examples:
import { randomGeometric } from "d3-random";
// Number of attempts until success (20% success rate)
const attempts = randomGeometric(0.2);
console.log(attempts()); // e.g., 7 attempts
// Time until first customer (10% probability per minute)
const waitTime = randomGeometric(0.1);
console.log(waitTime()); // e.g., 12 minutesGenerates binomially distributed random numbers, modeling the number of successes in n independent trials.
/**
* Returns a function for generating binomial distribution random numbers
* @param n - Number of trials (must be ≥ 0)
* @param p - Success probability per trial, must be in range [0, 1]
* @returns Generator function producing integers from 0 to n
*/
function randomBinomial(n: number, p: number): () => number;Usage Examples:
import { randomBinomial } from "d3-random";
// Number of heads in 10 coin flips
const heads = randomBinomial(10, 0.5);
console.log(heads()); // e.g., 6
// Number of defective items in batch of 100 (2% defect rate)
const defects = randomBinomial(100, 0.02);
console.log(defects()); // e.g., 1
// Survey responses (50 people, 30% response rate)
const responses = randomBinomial(50, 0.3);
console.log(responses()); // e.g., 14Install with Tessl CLI
npx tessl i tessl/npm-d3-random