Creating a unique alphanumeric 10-character string

前端 未结 7 2176
青春惊慌失措
青春惊慌失措 2020-12-06 12:32

I\'m looking to create a simple short-lived reservation system, and I\'d like to generate confirmation numbers that are

  • unique
  • random-looking
7条回答
  •  时光取名叫无心
    2020-12-06 12:37

    There should be no difference in the randomness of any given bit of a SHA-1 hash, so that's possible. Another way would be to fold the hash into itself using XOR until you have 60 bits worth of data, then encode it using Base 64 to get a mostly alpha-numeric result.

    This is only necessary if you want to be able to generate the same Id repeatedly for the same input data. Otherwise, if a random id that you generate once, and hold onto after that, use Anders' suggestion. If you get a conflict, just generate another one.

提交回复
热议问题