-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomInt.ts
38 lines (33 loc) · 898 Bytes
/
randomInt.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
/**
* Get a random integer within an inclusive range
* @param min the lower bound of the range
* @param max the upper bound of the range
* @returns a random integer between the bounds
*
* @example
* ```
* const value = randomInt(-10, 10)
* // value >= -10
* // value <= 10
* // Math.round(value) === value
* ```
*/
export const randomInt = (min: number, max: number) =>
Math.round((Math.random() * (max - min))) + min
// Tests
if (import.meta.vitest) {
const { it, expect } = import.meta.vitest
it('is always an integer', () => {
for (let i = 0; i < 10000; i++) {
const val = randomInt(-10, 10)
expect(val).toEqual(Math.round(val))
}
})
it('is always within range', () => {
for (let i = 0; i < 10000; i++) {
const val = randomInt(-10, 10)
expect(val).toBeGreaterThanOrEqual(-10)
expect(val).toBeLessThanOrEqual(10)
}
})
}