Create a unique 10-character alphanumeric string

I am looking to create a simple short-term reservation system, and I would like to generate confirmation numbers that

  • unique
  • random look
  • alphanumeric
  • short-ish, at least much shorter than the 32 character lines returned by sha1

I just want to have ~ 500 reservations, so I can’t imagine the high probability of collisions.

One of my ideas was to generate a sha1 hash based on a date stamp and username, and then truncate it to the first 10 characters. Would something like this be reliably unique for processing ~ 500 reservations?

+7
random unique
source share
7 answers

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

This is only necessary if you want the same identifier to be repeated for the same input. Otherwise, if the random identifier that you generate once and is held after that, use the Anders clause. If you run into a conflict, just create another one.

+4
source share

You can use any, even a simple random number generator; however, you must verify that the reservation code does not yet exist. If so, add the characters ('x') to the string (date + user) until you get a new random / sha 1 / etc.

I just want to have ~ 500 reservations, so I can’t imagine the high probability of collisions.

Another stupid idea: create unique random numbers 1000 or 2000 with the desired properties, save them somewhere and assign them to users when registering :)

+3
source share

Here is one way to do it in Perl:

  sub get_random_name ()
 {
   my @chars = ('a' .. 'z', 'A' .. 'Z');
   my $ random_string;

foreach (1..22) {# rand @chars will generate random # numbers from 0 to scalar @chars $ random_string. = $ chars [rand @chars]; } return $ random_string. "-". time(); }

I don’t remember how long it takes part of the time (), so you may have to adjust the numbers corresponding to your length. You can also remove this part if you do not need it.

+2
source share

If this is really only 500, then pre-generate 20,000 of them into a table, and then get the "next unused" when you need it.

+2
source share

Some useful tips on this: How to create an arbitrary alphanumeric string in C ++?

I would avoid including characters like "1", "l" and "O", "0" and "5", "S" and "Z", "2" in your line to make this easier for clients, when they need to read the reservation code by phone. The algorithm presented on this link should help you with this.

+1
source share

use a pointer? 16 characters, although if you really don't care about the collision, you can simply select the first n characters.

0
source share
0
source share

All Articles