UNPKG

4.94 kBTypeScriptView Raw
1import { Engine } from "./types";
2/**
3 * A wrapper around an Engine that provides easy-to-use methods for
4 * producing values based on known distributions
5 */
6export declare class Random {
7 private readonly engine;
8 /**
9 * Creates a new Random wrapper
10 * @param engine The engine to use (defaults to a `Math.random`-based implementation)
11 */
12 constructor(engine?: Engine);
13 /**
14 * Returns a value within [-0x80000000, 0x7fffffff]
15 */
16 int32(): number;
17 /**
18 * Returns a value within [0, 0xffffffff]
19 */
20 uint32(): number;
21 /**
22 * Returns a value within [0, 0x1fffffffffffff]
23 */
24 uint53(): number;
25 /**
26 * Returns a value within [0, 0x20000000000000]
27 */
28 uint53Full(): number;
29 /**
30 * Returns a value within [-0x20000000000000, 0x1fffffffffffff]
31 */
32 int53(): number;
33 /**
34 * Returns a value within [-0x20000000000000, 0x20000000000000]
35 */
36 int53Full(): number;
37 /**
38 * Returns a value within [min, max]
39 * @param min The minimum integer value, inclusive. No less than -0x20000000000000.
40 * @param max The maximum integer value, inclusive. No greater than 0x20000000000000.
41 */
42 integer(min: number, max: number): number;
43 /**
44 * Returns a floating-point value within [0.0, 1.0]
45 */
46 realZeroToOneInclusive(): number;
47 /**
48 * Returns a floating-point value within [0.0, 1.0)
49 */
50 realZeroToOneExclusive(): number;
51 /**
52 * Returns a floating-point value within [min, max) or [min, max]
53 * @param min The minimum floating-point value, inclusive.
54 * @param max The maximum floating-point value.
55 * @param inclusive If true, `max` will be inclusive.
56 */
57 real(min: number, max: number, inclusive?: boolean): number;
58 /**
59 * Returns a boolean with 50% probability of being true or false
60 */
61 bool(): boolean;
62 /**
63 * Returns a boolean with the provided `percentage` of being true
64 * @param percentage A number within [0, 1] of how often the result should be `true`
65 */
66 bool(percentage: number): boolean;
67 /**
68 * Returns a boolean with a probability of `numerator`/`denominator` of being true
69 * @param numerator The numerator of the probability
70 * @param denominator The denominator of the probability
71 */
72 bool(numerator: number, denominator: number): boolean;
73 /**
74 * Return a random value within the provided `source` within the sliced
75 * bounds of `begin` and `end`.
76 * @param source an array of items to pick from
77 * @param begin the beginning slice index (defaults to `0`)
78 * @param end the ending slice index (defaults to `source.length`)
79 */
80 pick<T>(source: ArrayLike<T>, begin?: number, end?: number): T;
81 /**
82 * Shuffles an array in-place
83 * @param array The array to shuffle
84 */
85 shuffle<T>(array: T[]): T[];
86 /**
87 * From the population array, returns an array with sampleSize elements that
88 * are randomly chosen without repeats.
89 * @param population An array that has items to choose a sample from
90 * @param sampleSize The size of the result array
91 */
92 sample<T>(population: ArrayLike<T>, sampleSize: number): T[];
93 /**
94 * Returns a value within [1, sideCount]
95 * @param sideCount The number of sides of the die
96 */
97 die(sideCount: number): number;
98 /**
99 * Returns an array of length `dieCount` of values within [1, sideCount]
100 * @param sideCount The number of sides of each die
101 * @param dieCount The number of dice
102 */
103 dice(sideCount: number, dieCount: number): number[];
104 /**
105 * Returns a Universally Unique Identifier Version 4.
106 *
107 * See http://en.wikipedia.org/wiki/Universally_unique_identifier
108 */
109 uuid4(): string;
110 /**
111 * Returns a random string using numbers, uppercase and lowercase letters,
112 * `_`, and `-` of length `length`.
113 * @param length Length of the result string
114 */
115 string(length: number): string;
116 /**
117 * Returns a random string using the provided string pool as the possible
118 * characters to choose from of length `length`.
119 * @param length Length of the result string
120 */
121 string(length: number, pool: string): string;
122 /**
123 * Returns a random string comprised of numbers or the characters `abcdef`
124 * (or `ABCDEF`) of length `length`.
125 * @param length Length of the result string
126 * @param uppercase Whether the string should use `ABCDEF` instead of `abcdef`
127 */
128 hex(length: number, uppercase?: boolean): string;
129 /**
130 * Returns a random `Date` within the inclusive range of [`start`, `end`].
131 * @param start The minimum `Date`
132 * @param end The maximum `Date`
133 */
134 date(start: Date, end: Date): Date;
135}