Last active
July 25, 2025 17:49
-
-
Save paceaux/d490cb9924f313077f7af4a49107d8ec to your computer and use it in GitHub Desktop.
Random String Generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Note: Note cryptographically secure. | |
function randomString(size = 5) { | |
const stringArray = [...Math.random().toString()].slice(2, 2 + size); | |
const intArray = stringArray.map(string => parseInt(string, 10)); | |
let chars = intArray.reduce((acc, c, idx) => { | |
const charPos = [65,97]; | |
let charStart = charPos[c%2]; | |
const charOffset = Math.ceil(Math.random() * 10)%3; | |
charStart = (charOffset * 8) + charStart; | |
return String.fromCharCode(c + charStart) + acc; | |
},[]); | |
if (chars.length < size) { | |
chars = chars + randomString(size - chars.length); | |
} | |
return chars; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment