0
0
mirror of https://github.com/sp-tarkov/server.git synced 2025-02-12 15:50:42 -05:00

Updates RandomUtil.randInt to return the input when the input values are the same.

Adds a test.
This commit is contained in:
Refringe 2024-12-09 11:42:40 -05:00
parent 90b6f28f31
commit 1074306758
Signed by: Refringe
SSH Key Fingerprint: SHA256:t865XsQpfTeqPRBMN2G6+N8wlDjkgUCZF3WGW6O9N/k
2 changed files with 11 additions and 4 deletions

View File

@ -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);
}
/**

View File

@ -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;