1 | declare abstract class RNG {
|
2 | abstract get name(): string;
|
3 | abstract next(): number;
|
4 | abstract clone(): RNG;
|
5 | }
|
6 |
|
7 | type RNGFn = () => number;
|
8 | type Seed = number | string;
|
9 | type SeedOrRNG = number | string | RNGFn | RNG;
|
10 |
|
11 | declare class ARC4RNG extends RNG {
|
12 | protected _seed: number;
|
13 | i: number;
|
14 | j: number;
|
15 | S: number[];
|
16 | constructor(seed?: Seed);
|
17 | get name(): string;
|
18 | next(): number;
|
19 | g(count: number): number;
|
20 | clone(): ARC4RNG;
|
21 | }
|
22 |
|
23 | declare class FunctionRNG extends RNG {
|
24 | _name: string;
|
25 | _rngFn: RNGFn;
|
26 | constructor(rngFn: RNGFn);
|
27 | get name(): string;
|
28 | next(): number;
|
29 | clone(): FunctionRNG;
|
30 | }
|
31 |
|
32 | declare class MathRandomRNG extends RNG {
|
33 | get name(): string;
|
34 | next(): number;
|
35 | clone(): MathRandomRNG;
|
36 | }
|
37 |
|
38 | declare class XOR128RNG extends RNG {
|
39 | protected _seed: number;
|
40 | x: number;
|
41 | y: number;
|
42 | z: number;
|
43 | w: number;
|
44 | constructor(seed?: Seed);
|
45 | get name(): string;
|
46 | next(): number;
|
47 | clone(): XOR128RNG;
|
48 | }
|
49 |
|
50 | /**
|
51 | * Distribution function
|
52 | */
|
53 | type IDistFn<R> = (random: Random, ...argv: any) => R;
|
54 | /**
|
55 | * Distribution
|
56 | */
|
57 | type IDist<R> = () => R;
|
58 | /**
|
59 | * Keyed cache entry
|
60 | */
|
61 | interface ICacheEntry<T> {
|
62 | key: string;
|
63 | distribution: () => T;
|
64 | }
|
65 | /**
|
66 | * Seedable random number generator supporting many common distributions.
|
67 | *
|
68 | * @name Random
|
69 | * @class
|
70 | *
|
71 | * @param {RNG|function|string|number} [rng=Math.random] - Underlying random number generator or a seed for the default PRNG. Defaults to `Math.random`.
|
72 | */
|
73 | declare class Random {
|
74 | protected _rng: RNG;
|
75 | protected readonly _cache: {
|
76 | [k: string]: ICacheEntry<any>;
|
77 | };
|
78 | constructor(seedOrRNG?: SeedOrRNG);
|
79 | /**
|
80 | * @member {RNG} rng - Underlying pseudo-random number generator.
|
81 | */
|
82 | get rng(): RNG;
|
83 | /**
|
84 | * Creates a new `Random` instance, optionally specifying parameters to
|
85 | * set a new seed.
|
86 | *
|
87 | * @return {Random}
|
88 | */
|
89 | clone(seedOrRNG?: SeedOrRNG): Random;
|
90 | /**
|
91 | * Sets the underlying pseudorandom number generator.
|
92 | *
|
93 | * @example
|
94 | * ```ts
|
95 | * import random from 'random'
|
96 | *
|
97 | * random.use('example-seed')
|
98 | * // or
|
99 | * random.use(Math.random)
|
100 | * ```
|
101 | */
|
102 | use(seedOrRNG: SeedOrRNG): void;
|
103 | /**
|
104 | * Convenience wrapper around `this.rng.next()`
|
105 | *
|
106 | * Returns a floating point number in [0, 1).
|
107 | *
|
108 | * @return {number}
|
109 | */
|
110 | next: () => number;
|
111 | /**
|
112 | * Samples a uniform random floating point number, optionally specifying
|
113 | * lower and upper bounds.
|
114 | *
|
115 | * Convence wrapper around `random.uniform()`
|
116 | *
|
117 | * @param {number} [min=0] - Lower bound (float, inclusive)
|
118 | * @param {number} [max=1] - Upper bound (float, exclusive)
|
119 | * @return {number}
|
120 | */
|
121 | float: (min?: number, max?: number) => number;
|
122 | /**
|
123 | * Samples a uniform random integer, optionally specifying lower and upper
|
124 | * bounds.
|
125 | *
|
126 | * Convence wrapper around `random.uniformInt()`
|
127 | *
|
128 | * @param {number} [min=0] - Lower bound (integer, inclusive)
|
129 | * @param {number} [max=1] - Upper bound (integer, inclusive)
|
130 | * @return {number}
|
131 | */
|
132 | int: (min?: number, max?: number) => number;
|
133 | /**
|
134 | * Samples a uniform random integer, optionally specifying lower and upper
|
135 | * bounds.
|
136 | *
|
137 | * Convence wrapper around `random.uniformInt()`
|
138 | *
|
139 | * @alias `random.int`
|
140 | *
|
141 | * @param {number} [min=0] - Lower bound (integer, inclusive)
|
142 | * @param {number} [max=1] - Upper bound (integer, inclusive)
|
143 | * @return {number}
|
144 | */
|
145 | integer: (min?: number, max?: number) => number;
|
146 | /**
|
147 | * Samples a uniform random boolean value.
|
148 | *
|
149 | * Convence wrapper around `random.uniformBoolean()`
|
150 | *
|
151 | * @alias `random.boolean`
|
152 | *
|
153 | * @return {boolean}
|
154 | */
|
155 | bool: () => boolean;
|
156 | /**
|
157 | * Samples a uniform random boolean value.
|
158 | *
|
159 | * Convence wrapper around `random.uniformBoolean()`
|
160 | *
|
161 | * @return {boolean}
|
162 | */
|
163 | boolean: () => boolean;
|
164 | /**
|
165 | * Returns an item chosen uniformly at random from the given array.
|
166 | *
|
167 | * Convence wrapper around `random.uniformInt()`
|
168 | *
|
169 | * @param {Array<T>} [array] - Input array
|
170 | * @return {T | undefined}
|
171 | */
|
172 | choice<T>(array: Array<T>): T | undefined;
|
173 | /**
|
174 | * Generates a [Continuous uniform distribution](https://en.wikipedia.org/wiki/Uniform_distribution_(continuous)).
|
175 | *
|
176 | * @param {number} [min=0] - Lower bound (float, inclusive)
|
177 | * @param {number} [max=1] - Upper bound (float, exclusive)
|
178 | */
|
179 | uniform: (min?: number, max?: number) => IDist<number>;
|
180 | /**
|
181 | * Generates a [Discrete uniform distribution](https://en.wikipedia.org/wiki/Discrete_uniform_distribution).
|
182 | *
|
183 | * @param {number} [min=0] - Lower bound (integer, inclusive)
|
184 | * @param {number} [max=1] - Upper bound (integer, inclusive)
|
185 | * @return {function}
|
186 | */
|
187 | uniformInt: (min?: number, max?: number) => IDist<number>;
|
188 | /**
|
189 | * Generates a [Discrete uniform distribution](https://en.wikipedia.org/wiki/Discrete_uniform_distribution),
|
190 | * with two possible outcomes, `true` or `false.
|
191 | *
|
192 | * This method is analogous to flipping a coin.
|
193 | *
|
194 | * @return {function}
|
195 | */
|
196 | uniformBoolean: () => IDist<boolean>;
|
197 | /**
|
198 | * Generates a [Normal distribution](https://en.wikipedia.org/wiki/Normal_distribution).
|
199 | *
|
200 | * @param {number} [mu=0] - Mean
|
201 | * @param {number} [sigma=1] - Standard deviation
|
202 | * @return {function}
|
203 | */
|
204 | normal: (mu?: number, sigma?: number) => () => number;
|
205 | /**
|
206 | * Generates a [Log-normal distribution](https://en.wikipedia.org/wiki/Log-normal_distribution).
|
207 | *
|
208 | * @param {number} [mu=0] - Mean of underlying normal distribution
|
209 | * @param {number} [sigma=1] - Standard deviation of underlying normal distribution
|
210 | * @return {function}
|
211 | */
|
212 | logNormal: (mu?: number, sigma?: number) => () => number;
|
213 | /**
|
214 | * Generates a [Bernoulli distribution](https://en.wikipedia.org/wiki/Bernoulli_distribution).
|
215 | *
|
216 | * @param {number} [p=0.5] - Success probability of each trial.
|
217 | * @return {function}
|
218 | */
|
219 | bernoulli: (p?: number) => () => number;
|
220 | /**
|
221 | * Generates a [Binomial distribution](https://en.wikipedia.org/wiki/Binomial_distribution).
|
222 | *
|
223 | * @param {number} [n=1] - Number of trials.
|
224 | * @param {number} [p=0.5] - Success probability of each trial.
|
225 | * @return {function}
|
226 | */
|
227 | binomial: (n?: number, p?: number) => () => number;
|
228 | /**
|
229 | * Generates a [Geometric distribution](https://en.wikipedia.org/wiki/Geometric_distribution).
|
230 | *
|
231 | * @param {number} [p=0.5] - Success probability of each trial.
|
232 | * @return {function}
|
233 | */
|
234 | geometric: (p?: number) => () => number;
|
235 | /**
|
236 | * Generates a [Poisson distribution](https://en.wikipedia.org/wiki/Poisson_distribution).
|
237 | *
|
238 | * @param {number} [lambda=1] - Mean (lambda > 0)
|
239 | * @return {function}
|
240 | */
|
241 | poisson: (lambda?: number) => () => number;
|
242 | /**
|
243 | * Generates an [Exponential distribution](https://en.wikipedia.org/wiki/Exponential_distribution).
|
244 | *
|
245 | * @param {number} [lambda=1] - Inverse mean (lambda > 0)
|
246 | * @return {function}
|
247 | */
|
248 | exponential: (lambda?: number) => () => number;
|
249 | /**
|
250 | * Generates an [Irwin Hall distribution](https://en.wikipedia.org/wiki/Irwin%E2%80%93Hall_distribution).
|
251 | *
|
252 | * @param {number} [n=1] - Number of uniform samples to sum (n >= 0)
|
253 | * @return {function}
|
254 | */
|
255 | irwinHall: (n?: number) => () => number;
|
256 | /**
|
257 | * Generates a [Bates distribution](https://en.wikipedia.org/wiki/Bates_distribution).
|
258 | *
|
259 | * @param {number} [n=1] - Number of uniform samples to average (n >= 1)
|
260 | * @return {function}
|
261 | */
|
262 | bates: (n?: number) => () => number;
|
263 | /**
|
264 | * Generates a [Pareto distribution](https://en.wikipedia.org/wiki/Pareto_distribution).
|
265 | *
|
266 | * @param {number} [alpha=1] - Alpha
|
267 | * @return {function}
|
268 | */
|
269 | pareto: (alpha?: number) => () => number;
|
270 | /**
|
271 | * Memoizes distributions to ensure they're only created when necessary.
|
272 | *
|
273 | * Returns a thunk which that returns independent, identically distributed
|
274 | * samples from the specified distribution.
|
275 | *
|
276 | * @internal
|
277 | *
|
278 | * @param {string} label - Name of distribution
|
279 | * @param {function} getter - Function which generates a new distribution
|
280 | * @param {...*} args - Distribution-specific arguments
|
281 | *
|
282 | * @return {function}
|
283 | */
|
284 | protected _memoize<T>(label: string, getter: IDistFn<any>, ...args: any[]): IDist<T>;
|
285 | }
|
286 | declare const _default: Random;
|
287 |
|
288 | declare function createRNG(seedOrRNG?: SeedOrRNG): RNG;
|
289 | declare function processSeed(seed?: Seed): number;
|
290 | declare function mixKey(seed: number, key: number[]): number[];
|
291 |
|
292 | export { ARC4RNG, FunctionRNG, MathRandomRNG, RNG, type RNGFn, Random, type Seed, type SeedOrRNG, XOR128RNG, createRNG, _default as default, mixKey, processSeed };
|