Skip to content

Instantly share code, notes, and snippets.

@harithzainudin
Created October 27, 2023 03:15
Show Gist options
  • Save harithzainudin/8577655e1f4c6d135dd5bb37bd82ea42 to your computer and use it in GitHub Desktop.
Save harithzainudin/8577655e1f4c6d135dd5bb37bd82ea42 to your computer and use it in GitHub Desktop.
Function to generate random characters according to the length provided
function generateUniqueID(length) {
const characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
let uniqueID = '';
while (uniqueID.length < length) {
const randomIndex = Math.floor(Math.random() * characters.length);
const randomChar = characters.charAt(randomIndex);
// Make sure the generated character is not already in the uniqueID
if (!uniqueID.includes(randomChar)) {
uniqueID += randomChar;
}
}
return uniqueID;
}
const uniqueID = generateUniqueID(5);
console.log(uniqueID);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment