-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.ts
47 lines (43 loc) · 1.23 KB
/
random.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
import { RandomNumberGenerator, Move, RandomMove, RandomSecretMove } from '.'
import { Seed } from './types'
/**
* A higher order function to provide `random`, a random number generator, to a move defintion.
* ```js
* withRandom(({ random, state }) => {
* const isHeads = random() > 0.5;
* return ({ ...state, isHeads })
* })
* ```
* @param moveFn A game move definition, which will require randomness
* @return A random move definition
*/
export function withRandom(moveFn: Move): RandomMove | RandomSecretMove {
moveFn._isRandom = true
return moveFn as RandomMove | RandomSecretMove
}
export const isRandomMove = (moveFn: Move) => moveFn._isRandom
export const shuffle = ({
random = Math.random,
array
}: {
random: RandomNumberGenerator
array: any[]
}) => {
const length = array == null ? 0 : array.length
if (!length) {
return []
}
let index = -1
const lastIndex = length - 1
const result = [...array]
while (++index < length) {
const rand = index + Math.floor(random() * (lastIndex - index + 1))
const value = result[rand]
result[rand] = result[index]
result[index] = value
}
return result
}
export function genSeed(random: RandomNumberGenerator): Seed {
return random().toString()
}