diff --git a/src/prelude/math.ts b/src/prelude/math.ts index 148cde9..04518ee 100644 --- a/src/prelude/math.ts +++ b/src/prelude/math.ts @@ -16,7 +16,10 @@ export const math = { if (n < 0) throw new Error(`sqrt: cannot take square root of negative number ${n}`) return Math.sqrt(n) }, - random: () => Math.random(), + random: (min = 0, max = 1) => { + if (min === 0 && max === 1) return Math.random() + return Math.floor(Math.random() * (max - min + 1)) + min + }, clamp: (n: number, min: number, max: number) => { if (min > max) throw new Error(`clamp: min (${min}) must be less than or equal to max (${max})`) return Math.min(Math.max(n, min), max)