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

Updates RandomUtil.randInt to handle floating-point parameters by flooring values. I hate this!

This commit is contained in:
Refringe 2024-12-09 20:21:06 -05:00
parent 1074306758
commit f278ff9294
Signed by: Refringe
SSH Key Fingerprint: SHA256:t865XsQpfTeqPRBMN2G6+N8wlDjkgUCZF3WGW6O9N/k
2 changed files with 25 additions and 4 deletions

View File

@ -298,7 +298,7 @@ export class RandomUtil {
/**
* Returns a random string from the provided array of strings.
*
* This method is separate from getArrayValue so we can use a generic inferance with getArrayValue.
* This method is separate from getArrayValue so we can use a generic inference with getArrayValue.
*
* @param arr - The array of strings to select a random value from.
* @returns A randomly selected string from the array.
@ -383,22 +383,38 @@ export class RandomUtil {
/**
* Generates a random integer between the specified range.
* Low and high parameters are floored to integers.
*
* TODO: v3.11 - This method should not accept non-integer numbers.
*
* @param low - The lower bound of the range (inclusive).
* @param high - The upper bound of the range (exclusive). If not provided, the range will be from 0 to `low`.
* @returns A random integer within the specified range.
*/
public randInt(low: number, high?: number): number {
let randomLow = low;
let randomHigh = high;
// Detect if either of the parameters is a float, and log a warning
if (low % 1 !== 0 || (typeof high !== "undefined" && high % 1 !== 0)) {
// Round the float values to the nearest integer. Eww!
randomLow = Math.floor(low);
if (typeof high !== "undefined") {
randomHigh = Math.floor(high);
}
}
// Return a random integer from 0 to low if high is not provided
if (typeof high === "undefined") {
return crypto.randomInt(0, low);
return crypto.randomInt(0, randomLow);
}
// Return low directly when low and high are equal
if (low === high) {
return low;
return randomLow;
}
return crypto.randomInt(low, high);
return crypto.randomInt(randomLow, randomHigh);
}
/**

View File

@ -399,6 +399,11 @@ describe("RandomUtil", () => {
expect(result).toBe(5);
});
it("should work with float number parameters", () => {
const result = randomUtil.randInt(5.5, 10.5);
expect(result).toBeGreaterThanOrEqual(5);
expect(result).toBeLessThan(11);
});
it("should return an integer between low and high - 1", () => {
const low = 5;
const high = 10;