aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils.ts
blob: 48771e90c6e8ed7e2764dcf1f4189f0fcf097e2b (plain) (blame)
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
/**
 * Returns a random number between min (inclusive) and max (exclusive)
 * @param min - minimum
 * @param max - maximum
 * @returns Random int
 */
export function getRandomInt(min: number, max: number): number {
  return Math.floor(Math.random() * (max - min) + min);
}
/**
  * Gets random element from array
  * @typeParam T - type of elements inside given array
  * @param arr - array
  * @returns Random element from given array
*/
export function getRandomEl<T>(arr: Array<T>): T {
  return arr[getRandomInt(0, arr.length)];
}
/**
  * Function that sleeps
  * @param time - sleep for this time [ms]
  * @returns Promise that resolves after `time`s
*/
export async function sleep(time: number) {
  return new Promise(resolve => setTimeout(resolve, time));
}