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
Common statistical distributions for general-purpose random number generation, suitable for most applications including simulations, games, and data visualization.
Generates random numbers with equal probability across a specified range.
/**
* Returns a function for generating uniform distribution random numbers
* @param min - Minimum value (inclusive), defaults to 0
* @param max - Maximum value (exclusive), defaults to 1
* @returns Generator function producing numbers in range [min, max)
*/
function randomUniform(min?: number, max?: number): () => number;Usage Examples:
import { randomUniform } from "d3-random";
// Default range [0, 1)
const random01 = randomUniform();
console.log(random01()); // e.g., 0.7234
// Custom range [1, 6) - like a die roll
const die = randomUniform(1, 6);
console.log(Math.floor(die())); // 1, 2, 3, 4, or 5
// Single parameter sets max, min defaults to 0
const zeroToTen = randomUniform(10);
console.log(zeroToTen()); // e.g., 7.123Generates random integers with uniform distribution across a specified range.
/**
* Returns a function for generating uniform integer random numbers
* @param min - Minimum integer value (inclusive), defaults to 0
* @param max - Maximum integer value (exclusive)
* @returns Generator function producing integers in range [⌊min⌋, ⌊max-1⌋]
*/
function randomInt(min?: number, max?: number): () => number;Usage Examples:
import { randomInt } from "d3-random";
// Roll a six-sided die (1-6)
const die = randomInt(1, 7);
console.log(die()); // 1, 2, 3, 4, 5, or 6
// Random index for array of length 10
const arrayIndex = randomInt(10);
console.log(arrayIndex()); // 0, 1, 2, ..., or 9
// Random between -5 and 5
const centered = randomInt(-5, 6);
console.log(centered()); // -5, -4, ..., 4, or 5Generates normally distributed random numbers with specified mean and standard deviation.
/**
* Returns a function for generating normal distribution random numbers
* @param mu - Mean (expected value), defaults to 0
* @param sigma - Standard deviation, defaults to 1
* @returns Generator function producing normally distributed numbers
*/
function randomNormal(mu?: number, sigma?: number): () => number;Usage Examples:
import { randomNormal } from "d3-random";
// Standard normal distribution (mean=0, stddev=1)
const standard = randomNormal();
console.log(standard()); // e.g., -0.234
// IQ scores (mean=100, stddev=15)
const iqScore = randomNormal(100, 15);
console.log(Math.round(iqScore())); // e.g., 103
// Height in inches (mean=68, stddev=4)
const height = randomNormal(68, 4);
console.log(height().toFixed(1)); // e.g., 71.2Generates log-normally distributed random numbers, useful for modeling multiplicative processes.
/**
* Returns a function for generating log-normal distribution random numbers
* @param mu - Mean of the underlying normal distribution, defaults to 0
* @param sigma - Standard deviation of the underlying normal distribution, defaults to 1
* @returns Generator function producing log-normally distributed numbers
*/
function randomLogNormal(mu?: number, sigma?: number): () => number;Usage Examples:
import { randomLogNormal } from "d3-random";
// Stock price movements
const stockPrice = randomLogNormal(0, 0.2);
console.log(stockPrice().toFixed(2)); // e.g., 1.15
// File sizes or network latencies
const fileSize = randomLogNormal(10, 1);
console.log(Math.round(fileSize())); // e.g., 18234Generates exponentially distributed random numbers, commonly used for modeling time between events.
/**
* Returns a function for generating exponential distribution random numbers
* @param lambda - Rate parameter (1/mean)
* @returns Generator function producing exponentially distributed numbers
*/
function randomExponential(lambda: number): () => number;Usage Examples:
import { randomExponential } from "d3-random";
// Time between customer arrivals (average 1 every 5 minutes)
const customerArrival = randomExponential(1/5);
console.log(customerArrival().toFixed(1)); // e.g., 3.2 minutes
// Server response times (average 100ms)
const responseTime = randomExponential(1/100);
console.log(Math.round(responseTime())); // e.g., 85 msGenerates Bates distributed random numbers (mean of n uniform random variables).
/**
* Returns a function for generating Bates distribution random numbers
* @param n - Number of independent uniform variables to average
* @returns Generator function producing Bates distributed numbers
*/
function randomBates(n: number): () => number;Usage Examples:
import { randomBates } from "d3-random";
// Approximate normal distribution using 10 uniform variables
const approximateNormal = randomBates(10);
console.log(approximateNormal().toFixed(3)); // e.g., 0.523
// More concentrated around 0.5 than uniform
const concentrated = randomBates(5);
console.log(concentrated().toFixed(3)); // e.g., 0.487Generates Irwin-Hall distributed random numbers (sum of n uniform random variables).
/**
* Returns a function for generating Irwin-Hall distribution random numbers
* @param n - Number of independent uniform variables to sum
* @returns Generator function producing Irwin-Hall distributed numbers
*/
function randomIrwinHall(n: number): () => number;Usage Examples:
import { randomIrwinHall } from "d3-random";
// Sum of 3 uniform variables (range 0-3)
const sum3 = randomIrwinHall(3);
console.log(sum3().toFixed(2)); // e.g., 1.75
// Approximate normal shape with integer parameter
const shaped = randomIrwinHall(12);
console.log(shaped().toFixed(2)); // e.g., 6.23Install with Tessl CLI
npx tessl i tessl/npm-d3-random