You have very few choice of letter, and you only generate 6 characters code, I made a simple script to check how many times do you have to generate the code until it matches with LS73ea
12787899 times
20141743 times
31479934 times
18819863 times
30361586 times
5533616 times
6495725 times
13431128 times
29765633 times
18367311 times
That's a quite high chances of duplicates.
I don't understand why would you include same characters in the letters
. Instead, do something like this:
methods: {
generateCode () {
// Alphanumeric
const characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
const uniqueId = function () {
let text = ''
for (let i = 0; i < 8; i++) {
text += characters.charAt(Math.floor(Math.random() * characters.length))
}
return text
}
return uniqueId()
}
}
That function will generate a code like HrsK9wgF
, and have way less chance of duplicates, and if you want to make it even lower, you can also:
- Add more characters like symbol (
-
, _
) to the choice of letters
- Increase the generated code length
As for reference, Youtube's video id used A-Z
, a-z
, 0-9
, -
and _
for the choice of characters, and has 11 characters length.
Apart from that, are you sure you want to generate the ID on the front end app? It seems like it should be generated on your backend app instead.