-
-
Notifications
You must be signed in to change notification settings - Fork 144
/
index.ts
174 lines (164 loc) · 5.03 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import type { Fn0 } from "@thi.ng/api";
import { B58_CHARS_LC } from "@thi.ng/base-n/chars/58";
import type { IRandom } from "@thi.ng/random";
import { DEFAULT_SEED_128 } from "@thi.ng/random/constants";
import { pickRandom, pickRandomKey } from "@thi.ng/random/pick-random";
import { SFC32 } from "@thi.ng/random/sfc32";
import { pickRandomUnique } from "@thi.ng/random/unique-indices";
import {
weightedRandom,
weightedRandomKey,
} from "@thi.ng/random/weighted-random";
declare var fxhash: string;
declare var $fx: { hash: string };
/**
* Takes a `fxhash` compatible base58 transaction hash, parses it into 4
* truncated 32bit uints and returns these partial seeds as array. This returned
* array can then be passed to reseed the {@link RND} instance.
*
* @remarks
* Note: The first characters of the given `hash` will ALWAYS be ignored (same
* logic fxhash is using & due to Tezos hash structure).
*
* Also see
* [`BASE58_LC`](https://docs.thi.ng/umbrella/base-n/variables/BASE58_LC.html).
*
* @example
* ```ts
* RND.seed(seedFromHash("ookorwLedQrCTPesBcUPrR2oRbPHgsAxe9xgCSNq4XAuZSaCvaB"));
* ```
*
* @param hash
*/
export const seedFromHash = (hash: string) =>
hash
.slice(2)
.match(new RegExp(`.{${(hash.length - 2) >> 2}}`, "g"))!
.map((x) =>
[...x].reduce(
(acc, y) => (acc * 58 + B58_CHARS_LC.indexOf(y)) >>> 0,
0
)
);
let seed: number[];
if (typeof fxhash === "string") {
seed = seedFromHash(fxhash);
} else if (typeof $fx?.hash === "string") {
seed = seedFromHash($fx.hash);
} else {
seed = DEFAULT_SEED_128;
console.warn("fxhash PRNG not found, using default seed", seed, "\n\n");
}
/**
* Singleton replacement for global `fxrand` PRNG function and pre-seeded with
* same global `fxhash` (both injected by fxhash's script in HTML header)
*
* @remarks
* Should the need arise, the instance can be reseeded via `.seed([...])`.
*/
export const RND = new SFC32(seed);
/**
* Returns true if the next random float from {@link RND} is less than given `p`.
*
* @param p
* @param rnd - default {@link RND}
*/
export const probability = (p: number, rnd: IRandom = RND) =>
rnd.probability(p);
/**
* Wrapper for
* [pickRandom()](https://docs.thi.ng/umbrella/random/functions/pickRandom.html).
* Returns a random item from given `choices`.
*
* @param choices
* @param rnd - default {@link RND}
*/
export const pick = <T>(choices: T[], rnd: IRandom = RND) =>
pickRandom(choices, rnd);
/**
* Wrapper for
* [pickRandomKey()](https://docs.thi.ng/umbrella/random/functions/pickRandomKey.html).
* Returns a random key from given `object`.
*
* @param obj
* @param rnd - default {@link RND}
*/
export const pickKey = <T extends object>(obj: T, rnd: IRandom = RND) =>
pickRandomKey(obj, rnd);
/**
* Repeatedly calls given zero-arg `fn` until it returns a different value than
* `orig` (using strict equality `===`).
*
* @remarks
* Also see {@link pickUnique} for selecting multiple unique items.
*
* @param fn
* @param orig
*/
export const pickAlt = <T>(fn: Fn0<T>, orig: T) => {
let res: T;
do {
res = fn();
} while (res === orig);
return res;
};
/**
* Same as thi.ng/random's [pickRandomUnique](), but pre-configured to use
* {@link RND} as default PRNG. Picks up to `k` unique values from `src` array
* (each with `maxTrials`) and adds them to given `existing` array (or creates a
* new one by default) and returns it.
*
* @remarks
* Internally uses `Array.includes()` to check for duplicates.
*
* @param k
* @param src
* @param existing
* @param maxTrials
* @param rnd
*/
export const pickUnique = <T>(
k: number,
src: T[],
existing?: T[],
maxTrials = 100,
rnd: IRandom = RND
) => pickRandomUnique(k, src, existing, maxTrials, rnd);
/**
* One-shot wrapper for
* [`weightedRandom()`](https://docs.thi.ng/umbrella/random/functions/weightedRandom.html).
* Takes an array of `choices` and an array of `weights`, returns random item
* from `choices`, taking relative weights into account.
*
* @remarks
* If repeated use (with the same choices/weights) is required, then using
* [`weightedRandom()`](https://docs.thi.ng/umbrella/random/functions/weightedRandom.html)
* directly is more efficient since the pre-calculations will be cached.
*
* @param choices
* @param weights
* @param rnd - default {@link RND}
*/
export const weighted = <T>(
choices: T[],
weights: number[],
rnd: IRandom = RND
) => weightedRandom(choices, weights, rnd)();
/**
* One-shot wrapper for
* [`weightedRandomKey()`](https://docs.thi.ng/umbrella/random/functions/weightedRandomKey.html),
* accepting an object of weights. Return keys of given `choices` object, taking
* into account the weights given for each key.
*
* @remarks
* If repeated use (with the same choices/weights) is required, then using
* [`weightedRandomKey()`](https://docs.thi.ng/umbrella/random/functions/weightedRandomKey.html)
* directly is more efficient since the pre-calculations will be cached.
*
* @param choices
* @param rnd
*/
export const weightedKey = <T extends Record<string, number>>(
choices: T,
rnd: IRandom = RND
) => weightedRandomKey(choices, rnd)();