diff --git a/project/src/utils/RandomUtil.ts b/project/src/utils/RandomUtil.ts index 1e07f269..fd09c548 100644 --- a/project/src/utils/RandomUtil.ts +++ b/project/src/utils/RandomUtil.ts @@ -389,14 +389,16 @@ export class RandomUtil { * @returns A random integer within the specified range. */ public randInt(low: number, high?: number): number { + if (typeof high === "undefined") { + return crypto.randomInt(0, low); + } + + // Return low directly when low and high are equal if (low === high) { return low; } - if (typeof high !== "undefined") { - return crypto.randomInt(low, high); - } - return crypto.randomInt(0, low); + return crypto.randomInt(low, high); } /** diff --git a/project/tests/utils/RandomUtil.test.ts b/project/tests/utils/RandomUtil.test.ts index e570df70..44ab463b 100644 --- a/project/tests/utils/RandomUtil.test.ts +++ b/project/tests/utils/RandomUtil.test.ts @@ -394,6 +394,11 @@ describe("RandomUtil", () => { }); describe("randInt", () => { + it("should return the same value when low and high are equal", () => { + const result = randomUtil.randInt(5, 5); + expect(result).toBe(5); + }); + it("should return an integer between low and high - 1", () => { const low = 5; const high = 10;