I'm trying to generate random unique User ID for one of my project and found Zerodha has very easy to remember user ids.
In Zerodha user ID: First two char's are Alphabets and then four char's are numbers.
How to make Id's Like Zerodha: "AC1940","AZ9940", "YM1300" etc.. ?
I wanted them to be unique and easy to remember.
Thank you.
You could generate random alphabets and number from ASCII code and Math.random() function and join them in a string.
const generateCharacter = (base, range) => String.fromCharCode(base + Math.floor(Math.random() * range)),
userId = Array.from({length: 2}, () => generateCharacter(65, 26)).join('') +
Array.from({length: 4}, () => generateCharacter(48, 10)).join('');
console.log(userId);
By respect of guys methods, those are not standard ways for unique random id. one of the standard and best ways to get random and standard methods for that is UUID.
EDITED:
you can use this uuid nodejs package to install and get the uuid unique random id corrctly.
You can create a random 6 digit number as
var randomNumber = (min, max) =>
Math.floor(Math.random() * (max - min + 1)) + min;
function generateUUID() {
const alphabets = [0, 1];
return Array.from({ length: 6 }, (_, i) => {
return alphabets.includes(i)
? String.fromCharCode(randomNumber(65, 90))
: randomNumber(0, 9);
}).join("");
}
console.log( generateUUID() );