Skip to content

Instantly share code, notes, and snippets.

@noudadrichem
Created October 4, 2019 10:23
Show Gist options
  • Select an option

  • Save noudadrichem/900831fd5c314cc4674858303397b5aa to your computer and use it in GitHub Desktop.

Select an option

Save noudadrichem/900831fd5c314cc4674858303397b5aa to your computer and use it in GitHub Desktop.
Return a random string with custom length from assigned characters
function getRandomString(wishedIdLength) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789=-[]\|';
var charactersLength = characters.length;
for (let i = 0; i < wishedIdLength; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
console.log(getRandomString(8));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment