Skip to content

Instantly share code, notes, and snippets.

@mudssrali
Created June 19, 2021 11:30
Show Gist options
  • Save mudssrali/50ea580dfb7e6f2ac5916664d58d9b3c to your computer and use it in GitHub Desktop.
Save mudssrali/50ea580dfb7e6f2ac5916664d58d9b3c to your computer and use it in GitHub Desktop.
Generate a random string of variable length
const generateRandomString = (stringLength) => {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
const charactersLength = characters.length
let randomString = ''
for (let i = 0; i < Math.abs(stringLength); i++) {
randomString += characters.charAt(Math.floor(Math.random() * charactersLength))
}
return randomString
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment