Skip to content

Commit 63784e2

Browse files
Merge pull request #120 from darrelfrancis/master
Resolve #81 to deal with a potential future scenario of PRNG generating `1`
2 parents 361eb27 + 5c14ac9 commit 63784e2

1 file changed

Lines changed: 5 additions & 5 deletions

File tree

source/utils.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { ENCODING, ENCODING_LEN } from "./constants.js";
22
import { PRNG } from "./types.js";
33

44
export function randomChar(prng: PRNG): string {
5-
let rand = Math.floor(prng() * ENCODING_LEN);
6-
if (rand === ENCODING_LEN) {
7-
rand = ENCODING_LEN - 1;
8-
}
9-
return ENCODING.charAt(rand);
5+
// Currently PRNGs generate fractions from 0 to _less than_ 1, so no "%" is necessary.
6+
// However, just in case a future PRNG can generate 1,
7+
// we are applying "% ENCODING LEN" to wrap back to the first character
8+
const randomPosition = Math.floor(prng() * ENCODING_LEN) % ENCODING_LEN;
9+
return ENCODING.charAt(randomPosition);
1010
}
1111

1212
export function replaceCharAt(str: string, index: number, char: string): string {

0 commit comments

Comments
 (0)