more random funcs
This commit is contained in:
parent
0fe73b6f3e
commit
010730127b
|
|
@ -20,3 +20,15 @@ export function rng(min: number, max = 0) {
|
|||
max = Math.floor(max)
|
||||
return Math.floor(Math.random() * (max - min + 1)) + min
|
||||
}
|
||||
|
||||
// randomElement([5, 7, 9]) #=> 7
|
||||
export function randomElement<T>(list: T[]): T | undefined {
|
||||
if (!list.length) return
|
||||
return list[rng(0, list.length - 1)]
|
||||
}
|
||||
|
||||
// randomIndex([5, 7, 9]) #=> 1
|
||||
export function randomIndex<T>(list: T[]): number | undefined {
|
||||
if (!list.length) return
|
||||
return rng(0, list.length - 1)
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user