-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrand.ts
29 lines (24 loc) · 925 Bytes
/
rand.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
import * as R from "ramda";
import * as seedrandom from "seedrandom";
import logger from "./logger";
export const resetGenerator = () => {
generator = seedrandom(process.env.E2E ? "some value" : undefined);
return generator;
};
let generator: ReturnType<typeof seedrandom> = resetGenerator();
export const rand = (min: number, max: number) =>
Math.floor(generator() * (max + 1 - min)) + min;
export const diceRoll = (fromPoints: number, toPoints: number): [number[], number[], boolean] => {
const fromRoll = R.range(0, fromPoints).map(_ => rand(1, 6));
const toRoll = R.range(0, toPoints).map(_ => rand(1, 6));
const success = R.sum(fromRoll) > R.sum(toRoll);
return [fromRoll, toRoll, success];
};
export const shuffle = <T>(a: readonly T[]): readonly T[] => {
const b = a.slice();
for (let i = b.length - 1; i > 0; i--) {
const j = rand(0, i);
[b[i], b[j]] = [b[j], b[i]];
}
return b;
};