mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 11:10:24 +00:00
pseudorandom.ts minor fixes (#2046)
## Description: in src/core/pseudorandom.ts a helper function of nextint is defined but never used. instead, this.rng is used. those are replaced. also, shuffleArray originally would self-shuffle at i=0 for the conditional being i>=0, fixed to 0. ## Please complete the following: - [X] I have added screenshots for all UI updates - [X] I process any text displayed to the user through translateText() and I've added it to the en.json file - [X] I have added relevant tests to the test directory - [X] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced ## Please put your Discord username so you can be contacted if a bug or regression is found: jack_45183
This commit is contained in:
@@ -36,7 +36,7 @@ export class PseudoRandom {
|
||||
if (arr.length === 0) {
|
||||
throw new Error("array must not be empty");
|
||||
}
|
||||
return arr[Math.floor(this.rng() * arr.length)];
|
||||
return arr[this.nextInt(0, arr.length)];
|
||||
}
|
||||
|
||||
// Selects a random element from a set.
|
||||
@@ -61,14 +61,14 @@ export class PseudoRandom {
|
||||
|
||||
// Returns true with probability 1/odds.
|
||||
chance(odds: number): boolean {
|
||||
return Math.floor(this.rng() * odds) === 0;
|
||||
return this.nextInt(0, odds) === 0;
|
||||
}
|
||||
|
||||
// Returns a shuffled copy of the array using Fisher-Yates algorithm.
|
||||
shuffleArray<T>(array: T[]): T[] {
|
||||
const result = [...array];
|
||||
for (let i = result.length - 1; i >= 0; i--) {
|
||||
const j = Math.floor(this.rng() * (i + 1));
|
||||
for (let i = result.length - 1; i > 0; i--) {
|
||||
const j = this.nextInt(0, i + 1);
|
||||
[result[i], result[j]] = [result[j], result[i]];
|
||||
}
|
||||
return result;
|
||||
|
||||
Reference in New Issue
Block a user