(min: number, max: number)
| 58 | * @param max - Exclusive upper bound |
| 59 | */ |
| 60 | export function randomInt(min: number, max: number): number { |
| 61 | const range = max - min |
| 62 | if (range <= 0) throw new RangeError(`randomInt: max (${max}) must be greater than min (${min})`) |
| 63 | const threshold = (0x100000000 - (0x100000000 % range)) >>> 0 |
| 64 | let value: number |
| 65 | do { |
| 66 | ;[value] = crypto.getRandomValues(new Uint32Array(1)) |
| 67 | } while (value >= threshold) |
| 68 | return min + (value % range) |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Returns a uniformly random element from a non-empty array. |
no outgoing calls
no test coverage detected