d3-random
从各种分布中生成随机数。有关种子随机数生成,请参阅 random.source 和 randomLcg。
¥Generate random numbers from various distributions. For seeded random number generation, see random.source and randomLcg.
randomUniform(min, max)
d3.randomUniform(6) // generate numbers ≥0 and <6
示例 · 源代码 · 返回一个使用 均匀分布 函数生成随机数的函数。返回数字的最小允许值为 min(含),最大值为 max(不含)。如果未指定 min,则默认为 0;如果未指定 max,则默认为 1。例如:
¥Examples · Source · 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:
randomInt(min, max)
d3.randomInt(100) // generate integers ≥0 and <100
示例 · 源代码 · 返回一个使用 均匀分布 函数生成随机整数的函数。返回数字的最小允许值为 ⌊min⌋(含),最大值为 ⌊max - 1⌋(含)。如果未指定 min,则默认为 0。例如:
¥Examples · Source · 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:
randomNormal(mu, sigma)
d3.randomNormal(0, 1) // mean of 0, and standard deviation of 1
示例 · 源代码 · 返回一个使用 正态(高斯)分布 函数生成随机数的函数。生成数字的期望值为 mu,标准差为 sigma。如果未指定 mu,则默认为 0;如果未指定 sigma,则默认为 1。
¥Examples · Source · 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.
randomLogNormal(mu, sigma)
d3.randomLogNormal(0, 1)
示例 · 源代码 · 返回一个使用 对数正态分布 函数生成随机数的函数。随机变量的自然对数的期望值为 mu,标准差为 sigma。如果未指定 mu,则默认为 0;如果未指定 sigma,则默认为 1。
¥Examples · Source · 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.
randomBates(n)
d3.randomBates(3) // generates numbers between 0 and 1
示例 · 源代码 · 返回一个函数,该函数使用带有 n 个独立变量的 贝茨分布 函数生成随机数。n 的小数部分处理方式与 d3.randomIrwinHall 类似,d3.randomBates(0) 等同于 d3.randomUniform()。
¥Examples · Source · 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().
randomIrwinHall(n)
d3.randomIrwinHall(3) // generates numbers between 0 and 3
示例 · 源代码 · 返回一个使用带有 n 个独立变量的 Irwin–Hall 分布 函数生成随机数的函数。如果 n 的小数部分非零,则将其视为将 d3.randomUniform() 乘以该小数部分的值添加到整数部分。
¥Examples · Source · 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.
randomExponential(lambda)
d3.randomExponential(1 / 40)
示例 · 源代码 · 返回一个使用速率为 lambda 的 指数分布 函数生成随机数的函数;相当于 泊松过程 中事件间隔时间,平均值为 1 / lambda。例如,randomExponential(1 / 40) 会在事件之间生成随机时间,平均每 40 个时间单位发生一次事件。
¥Examples · Source · 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, randomExponential(1 / 40) generates random times between events where, on average, one event occurs every 40 units of time.
randomPareto(alpha)
d3.randomPareto(6)
示例 · 源代码 · 返回一个函数,该函数使用形状为 alpha 的 帕累托分布 函数生成随机数。值 alpha 必须为正值。
¥Examples · Source · Returns a function for generating random numbers with a Pareto distribution with the shape alpha. The value alpha must be a positive value.
randomBernoulli(p)
d3.randomBernoulli(0.5)
示例 · 源代码 · 返回一个根据 伯努利分布 函数生成 1 或 0 的函数,成功概率为 p,返回 1;失败概率为 q = 1,返回 0。 - p。值 p 在 [0, 1] 范围内。
¥Examples · Source · 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].
randomGeometric(p)
d3.randomGeometric(0.1)
示例 · 源代码 · 返回一个使用 几何分布 函数生成数字的函数,成功概率为 p。p 值在 [0, 1] 范围内。
¥Examples · Source · Returns a function for generating numbers with a geometric distribution with success probability p. The value p is in the range [0, 1].
randomBinomial(n, p)
d3.randomBinomial(40, 0.5)
示例 · 源代码 · 返回一个函数,该函数使用试验次数为 n、每次试验成功概率为 p 的 二项分布 函数生成随机数。值 n 大于或等于 0,p 值在 [0, 1] 范围内。
¥Examples · Source · 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].
randomGamma(k, theta)
d3.randomGamma(2, 1)
示例 · 源代码 · 返回一个函数,该函数使用形状参数为 k、尺度参数为 theta 的 伽马分布 函数生成随机数。值 k 必须为正值;如果未指定 theta,则默认为 1。
¥Examples · Source · 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.
randomBeta(alpha, beta)
d3.randomBeta(3, 1.5)
示例 · 源代码 · 返回一个使用 β分布 函数生成随机数的函数,其 alpha 和 beta 形状参数必须均为正数。
¥Examples · Source · Returns a function for generating random numbers with a beta distribution with alpha and beta shape parameters, which must both be positive.
randomWeibull(k, a, b)
d3.randomWeibull(10)
示例 · 源代码 · 返回一个使用 广义极值分布 函数之一生成随机数的函数,该函数取决于 k:
¥Examples · Source · Returns a function for generating random numbers with one of the generalized extreme value distributions, depending on k:
如果 k 为正数,则返回形状参数为 k 的 威布尔分布。
¥If k is positive, the Weibull distribution with shape parameter k
如果 k 为零,则返回 Gumbel 分布。
¥If k is zero, the Gumbel distribution
如果 k 为负数,则返回形状参数为 −k 的 Fréchet 分布。
¥If k is negative, the Fréchet distribution with shape parameter −k
在这三种情况下,a 是位置参数,b 是比例参数。如果未指定 a,则默认为 0;如果未指定 b,则默认为 1。
¥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.
randomCauchy(a, b)
d3.randomCauchy(0, 1) // above, clipped to [-5, 5] because “fat tails”
示例 · 源代码 · 返回一个使用 柯西分布 函数生成随机数的函数。a 和 b 与 d3.randomWeibull 中的含义和默认值相同。
¥Examples · Source · 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.
randomLogistic(a, b)
d3.randomLogistic(0, 1)
示例 · 源代码 · 返回一个使用 逻辑分布 函数生成随机数的函数。a 和 b 与 d3.randomWeibull 中的含义和默认值相同。
¥Examples · Source · 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.
randomPoisson(lambda)
d3.randomPoisson(400)
示例 · 源代码 · 返回一个函数,该函数使用均值为 lambda 的 泊松分布分布 函数生成随机数。
¥Examples · Source · Returns a function for generating random numbers with a Poisson distribution with mean lambda.
random.source(source) {#random_source}
const seed = 0.44871573888282423; // any number in [0, 1)
const random = d3.randomNormal.source(d3.randomLcg(seed))(0, 1);
random(); // -0.6253955998897069
示例 · 返回用于生成随机数的同类型函数,但使用给定的随机数生成器源而不是 Math.random 作为随机数源。给定的随机数生成器必须实现与 Math.random 相同的接口,并且仅返回 [0, 1) 范围内的值。当种子随机数生成器优于 Math.random 时,这很有用。
¥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.
randomLcg(seed)
d3.randomLcg(42)
示例 · 源代码 · 返回一个 线性同余生成器 函数;此函数可以重复调用,以获取在区间 [0,1) 上均匀分布且周期较长(最多 10 亿个数字)的伪随机值,类似于 Math.random。种子可以指定为区间 [0,1) 内的实数或任意整数。在后一种情况下,仅考虑低 32 位。Two generators instanced with the same seed generate the same sequence, allowing to create reproducible pseudo-random experiments.如果未指定种子,则使用 Math.random 选择一个。
¥Examples · Source · 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.