shared rng function

This commit is contained in:
Chris Wanstrath 2025-09-27 19:00:52 -07:00
parent 5fb3b0333d
commit 557e2a8a8d

View File

@ -6,4 +6,17 @@ export function countChar(str: string, char: string): number {
// Generate a 6 character random ID
export function randomId(): string {
return Math.random().toString(36).slice(7)
}
// rng(1, 5) #=> result can be 1 2 3 4 or 5
// rng(2) #=> result can be 1 or 2
export function rng(min: number, max = 0) {
if (max === 0) {
max = min
min = 1
}
min = Math.ceil(min)
max = Math.floor(max)
return Math.floor(Math.random() * (max - min + 1)) + min
}