Skip to content
Merged
Changes from 1 commit
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