0

I try like this :

methods: {
    generateCode () {
      const letters = '1217CHELS323412eafoo'
      const uniqueId = function () {
        let text = ''
        for (let i = 0; i < 6; i++) {
          text += letters.charAt(Math.floor(Math.random() * letters.length))
        }
        return text
      }
      return uniqueId()
    }
}

Some examples of the results: : L1LE3C, o23H1f, HeLe34

Is this code perfect? I mean every time this method is called, it will definitely generate a unique booking code

moses toh
  • 12,344
  • 71
  • 243
  • 443
  • I recommend using [nanoid](https://www.npmjs.com/package/nanoid). Very easy to integrate and has a much lower change of duplicates – Luke Hol Jun 29 '20 at 16:03
  • 1
    This is not sure that it will generate a unique ID. You can add time and hash it. Or you can check if the code generated is already generated in your db before using it. – Player1 Jun 29 '20 at 16:23
  • @Player1 If I have to check the database, it seems better to generate it in the backend – moses toh Jun 29 '20 at 22:05
  • @Luke Hol Okay I will check it – moses toh Jun 29 '20 at 22:06

1 Answers1

2

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:

  1. Add more characters like symbol (-, _) to the choice of letters
  2. 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.

Owl
  • 6,337
  • 3
  • 16
  • 30
  • `const letters = '1217CHELS323412eafoo'` is taken from the user's input. it is a combination of reservation date, email, mobile phone and card number. So I must change it to `const letters = 'abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ-1234567890'`? – moses toh Jun 29 '20 at 22:03
  • Yes if you want to include `-` and `_`, or you can just use `abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890` and make the generated code longer, like 10 or 11 characters – Owl Jun 29 '20 at 22:07
  • Okay. Do I need `var timestamp = new Date().getUTCMilliseconds();` like this : https://stackoverflow.com/questions/8012002/create-a-unique-number-with-javascript-time? – moses toh Jun 29 '20 at 22:10
  • It's totally up to you and depends on your use case, if you want to include date/time on the booking code, then you can add that and mix with it your current method, so the code will look like this `20200630a1b2c3d4` (Mixing date 30-06-2020 with random alphanumeric), but if you just want a random unique code and want to make sure there are no duplicates, then generating alphanumeric with at least 8 characters length will be enough – Owl Jun 29 '20 at 22:14
  • Okay thanks. Seems I will using this : `const letters = 'abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ-1234567890'` – moses toh Jun 29 '20 at 22:23