Skip to content

Instantly share code, notes, and snippets.

@djhouseknecht
Last active March 24, 2023 14:01
Show Gist options
  • Save djhouseknecht/257c1590e00adbcd47188ffae884e356 to your computer and use it in GitHub Desktop.
Save djhouseknecht/257c1590e00adbcd47188ffae884e356 to your computer and use it in GitHub Desktop.
Generate a string of random characters
const chars = [
'a', 'A',
'b', 'B',
'c', 'C',
'd', 'D',
'e', 'E',
'f', 'F',
'g', 'G',
'h', 'H',
'i', 'I',
'j', 'J',
'k', 'K',
'l', 'L',
'm', 'M',
'n', 'N',
'o', 'O',
'p', 'P',
'q', 'Q',
'r', 'R',
's', 'S',
't', 'T',
'u', 'U',
'v', 'V',
'w', 'W',
'x', 'X',
'y', 'Y',
'z', 'Z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
];
function getRandomString (length) {
let str = '';
for (let i = 0; i < length; i++) {
const index = Math.floor(Math.random() * chars.length);
str += chars[index];
}
return str;
}
function runLoop(count, len = 5) {
const results = [];
for (let c = 0; c < count; c++) {
results.push(getRandomString(len));
}
return results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment