UNPKG

11.6 kBTypeScriptView Raw
1// Last module patch version validated against: 3.0.1
2
3export interface RandomNumberGenerationSource {
4 /**
5 * Returns the same type of function for generating random numbers but where the given random number
6 * generator source is used as the source of randomness instead of Math.random.
7 * This is useful when a seeded random number generator is preferable to Math.random.
8 *
9 * @param source Source (pseudo-)random number generator implementing the Math.random interface.
10 * The given random number generator must implement the same interface as Math.random and
11 * only return values in the range [0, 1).
12 */
13 source(source: () => number): this;
14}
15
16/**
17 * A configurable random number generator for the uniform distribution.
18 */
19export interface RandomUniform extends RandomNumberGenerationSource {
20 /**
21 * Returns a function for generating random numbers with a uniform distribution.
22 * The minimum allowed value of a returned number is min (inclusive), and the maximum is max (exclusive).
23 * Min defaults to 0; if max is not specified, it defaults to 1.
24 *
25 * @param max The maximum allowed value of a returned number, defaults to 1.
26 */
27 (max?: number): () => number;
28 /**
29 * Returns a function for generating random numbers with a uniform distribution.
30 * The minimum allowed value of a returned number is min (inclusive), and the maximum is max (exclusive).
31 *
32 * @param min The minimum allowed value of a returned number.
33 * @param max The maximum allowed value of a returned number.
34 */
35 // tslint:disable-next-line:unified-signatures
36 (min: number, max: number): () => number;
37}
38
39export const randomUniform: RandomUniform;
40
41/**
42 * A configurable random integer generator for the uniform distribution.
43 */
44export interface RandomInt extends RandomNumberGenerationSource {
45 /**
46 * Returns a function for generating random integers with a uniform distribution.
47 * The minimum allowed value of a returned number is ⌊min⌋ (inclusive), and the maximum is ⌊max - 1⌋ (inclusive)
48 * Min defaults to 0.
49 *
50 * @param max The maximum allowed value of a returned number.
51 */
52 (max: number): () => number;
53 /**
54 * Returns a function for generating random integers with a uniform distribution.
55 * The minimum allowed value of a returned number is ⌊min⌋ (inclusive), and the maximum is ⌊max - 1⌋ (inclusive)
56 *
57 * @param min The minimum allowed value of a returned number.
58 * @param max The maximum allowed value of a returned number.
59 */
60 // tslint:disable-next-line:unified-signatures
61 (min: number, max: number): () => number;
62}
63
64export const randomInt: RandomInt;
65
66/**
67 * A configurable random number generator for the normal (Gaussian) distribution.
68 */
69export interface RandomNormal extends RandomNumberGenerationSource {
70 /**
71 * Returns a function for generating random numbers with a normal (Gaussian) distribution.
72 * The expected value of the generated numbers is mu, with the given standard deviation sigma.
73 * If mu is not specified, it defaults to 0; if sigma is not specified, it defaults to 1.
74 *
75 * @param mu Expected value, defaults to 0.
76 * @param sigma Standard deviation, defaults to 1.
77 */
78 (mu?: number, sigma?: number): () => number;
79}
80
81export const randomNormal: RandomNormal;
82
83/**
84 * A configurable random number generator for the log-normal distribution.
85 */
86export interface RandomLogNormal extends RandomNumberGenerationSource {
87 /**
88 * Returns a function for generating random numbers with a log-normal distribution. The expected value of the random variable’s natural logarithm is mu,
89 * with the given standard deviation sigma. If mu is not specified, it defaults to 0; if sigma is not specified, it defaults to 1.
90 *
91 * @param mu Expected value, defaults to 0.
92 * @param sigma Standard deviation, defaults to 1.
93 */
94 (mu?: number, sigma?: number): () => number;
95}
96
97export const randomLogNormal: RandomLogNormal;
98
99/**
100 * A configurable random number generator for the Bates distribution.
101 */
102export interface RandomBates extends RandomNumberGenerationSource {
103 /**
104 * Returns a function for generating random numbers with a Bates distribution with n independent variables.
105 * The case of fractional n is handled as with d3.randomIrwinHall, and d3.randomBates(0) is equivalent to d3.randomUniform().
106 *
107 * @param n Number of independent random variables to use.
108 */
109 (n: number): () => number;
110}
111
112export const randomBates: RandomBates;
113
114/**
115 * A configurable random number generator for the Irwin–Hall distribution.
116 */
117export interface RandomIrwinHall extends RandomNumberGenerationSource {
118 /**
119 * Returns a function for generating random numbers with an Irwin–Hall distribution with n independent variables.
120 * If the fractional part of n is non-zero, this is treated as adding d3.randomUniform() times that fractional part to the integral part.
121 *
122 * @param n Number of independent random variables to use.
123 */
124 (n: number): () => number;
125}
126
127export const randomIrwinHall: RandomIrwinHall;
128
129/**
130 * A configurable random number generator for the exponential distribution.
131 */
132export interface RandomExponential extends RandomNumberGenerationSource {
133 /**
134 * Returns a function for generating random numbers with an exponential distribution with the rate lambda;
135 * equivalent to time between events in a Poisson process with a mean of 1 / lambda.
136 *
137 * @param lambda Expected time between events.
138 */
139 (lambda: number): () => number;
140}
141
142export const randomExponential: RandomExponential;
143
144/**
145 * A configurable random number generator with an Pareto distribution.
146 */
147export interface RandomPareto extends RandomNumberGenerationSource {
148 /**
149 * Returns a function for generating random numbers with a Pareto distribution with the shape alpha.
150 * The value alpha must be a positive value.
151 *
152 * @param alpha alpha
153 */
154 (alpha: number): () => number;
155}
156
157export const randomPareto: RandomPareto;
158
159/**
160 * A configurable random 0 or 1 generator according to a Bernoulli distribution.
161 */
162export interface RandomBernoulli extends RandomNumberGenerationSource {
163 /**
164 * 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.
165 * The value p is in the range [0, 1].
166 *
167 * @param p p
168 */
169 (p: number): () => number;
170}
171
172export const randomBernoulli: RandomBernoulli;
173
174/**
175 * A configurable random number generator with a geometric distribution.
176 */
177export interface RandomGeometric extends RandomNumberGenerationSource {
178 /**
179 * Returns a function for generating numbers with a geometric distribution with success probability p.
180 * The value p is in the range [0, 1].
181 *
182 * @param p Success probability
183 */
184 (p: number): () => number;
185}
186
187export const randomGeometric: RandomGeometric;
188
189/**
190 * A configurable random number generator with a binomial distribution.
191 */
192export interface RandomBinomial extends RandomNumberGenerationSource {
193 /**
194 * Returns a function for generating numbers with a geometric distribution with success probability p.
195 * The value p is in the range (0, 1].
196 *
197 * @param p Success probability
198 */
199 (p: number): () => number;
200}
201
202export const randomBinomial: RandomBinomial;
203
204/**
205 * A configurable random number generator with a gamma distribution.
206 */
207export interface RandomGamma extends RandomNumberGenerationSource {
208 /**
209 * Returns a function for generating random numbers with a gamma distribution with k the shape parameter and theta the scale parameter.
210 * The value k must be a positive value; if theta is not specified, it defaults to 1.
211 *
212 * @param k Shape parameter
213 * @param theta Scale paramter
214 */
215 (k: number, theta?: number): () => number;
216}
217
218export const randomGamma: RandomGamma;
219
220/**
221 * A configurable random number generator with a beta distribution.
222 */
223export interface RandomBeta extends RandomNumberGenerationSource {
224 /**
225 * Returns a function for generating random numbers with a beta distribution with alpha and beta shape parameters, which must both be positive.
226 *
227 * @param alpha Shape parameter
228 * @param beta Shape paramter
229 */
230 (alpha: number, beta: number): () => number;
231}
232
233export const randomBeta: RandomBeta;
234
235/**
236 * A configurable random number generator with one of the generalized extreme value distributions.
237 */
238export interface RandomWeibull extends RandomNumberGenerationSource {
239 /**
240 * Returns a function for generating random numbers with one of the generalized extreme value distributions, depending on k:
241 * If k is positive, the Weibull distribution with shape parameter k
242 * If k is zero, the Gumbel distribution
243 * If k is negative, the Fréchet distribution with shape parameter −k
244 * In all three cases, a is the location parameter and b is the scale parameter.
245 * If a is not specified, it defaults to 0; if b is not specified, it defaults to 1.
246 *
247 * @param k Shape parameter
248 * @param a Location parameter
249 * @param b Scale parameter
250 */
251 (k: number, a?: number, b?: number): () => number;
252}
253
254export const randomWeibull: RandomWeibull;
255
256/**
257 * A configurable random number generator with a Cauchy distribution.
258 */
259export interface RandomCauchy extends RandomNumberGenerationSource {
260 /**
261 * Returns a function for generating random numbers with a Cauchy distribution.
262 * a and b have the same meanings and default values as in d3.randomWeibull.
263 *
264 * @param a Location parameter
265 * @param b Scale parameter
266 */
267 (a?: number, b?: number): () => number;
268}
269
270export const randomCauchy: RandomCauchy;
271
272/**
273 * A configurable random number generator with a logistic distribution.
274 */
275export interface RandomLogistic extends RandomNumberGenerationSource {
276 /**
277 * Returns a function for generating random numbers with a logistic distribution.
278 * a and b have the same meanings and default values as in d3.randomWeibull.
279 *
280 * @param a Location parameter
281 * @param b Scale parameter
282 */
283 (a?: number, b?: number): () => number;
284}
285
286export const randomLogistic: RandomLogistic;
287
288/**
289 * A configurable random number generator with a Poisson distribution.
290 */
291export interface RandomPoisson extends RandomNumberGenerationSource {
292 /**
293 * Returns a function for generating random numbers with a Poisson distribution with mean lambda.
294 *
295 * @param lambda Mean
296 */
297 (lambda: number): () => number;
298}
299
300export const randomPoisson: RandomPoisson;
301
302/**
303 * Returns a linear congruential generator;
304 * 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.
305 * A seed can be specified as a real number in the interval [0,1) or as any integer.
306 * In the latter case, only the lower 32 bits are considered.
307 * Two generators instanced with the same seed generate the same sequence, allowing to create reproducible pseudo-random experiments.
308 * If the seed is not specified, one is chosen using Math.random.
309 *
310 * @param seed A seed that is either a real number in the interval [0,1) or any integer.
311 */
312export function randomLcg(seed?: number): () => number;