Skip to content

Resolve #81 to deal with a potential future scenario of PRNG generating 1 #120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions source/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { ENCODING, ENCODING_LEN } from "./constants.js";
import { PRNG } from "./types.js";

export function randomChar(prng: PRNG): string {
let rand = Math.floor(prng() * ENCODING_LEN);
if (rand === ENCODING_LEN) {
rand = ENCODING_LEN - 1;
}
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(randPosition);
}

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