We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 361eb27 + 5c14ac9 commit 63784e2Copy full SHA for 63784e2
1 file changed
source/utils.ts
@@ -2,11 +2,11 @@ import { ENCODING, ENCODING_LEN } from "./constants.js";
2
import { PRNG } from "./types.js";
3
4
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);
+ // Currently PRNGs generate fractions from 0 to _less than_ 1, so no "%" is necessary.
+ // However, just in case a future PRNG can generate 1,
+ // we are applying "% ENCODING LEN" to wrap back to the first character
+ const randomPosition = Math.floor(prng() * ENCODING_LEN) % ENCODING_LEN;
+ return ENCODING.charAt(randomPosition);
10
}
11
12
export function replaceCharAt(str: string, index: number, char: string): string {
0 commit comments