8

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

  • unique
  • random-looking
  • alphanumeric
  • short-ish, at least much shorter than 32 character-long strings returned by sha1

I'm only looking to have ~500 reservations, so I don't imagine high likelyhood of collissions.

One idea I had is generate an sha1 hash based on a date-time stamp and username, then truncating it to its first 10 characters. Would something like that be reliably unique enough for the purposes of processing ~500 reservations?

saturdayplace
  • 8,370
  • 8
  • 35
  • 39

7 Answers7

4

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.

Eclipse
  • 44,851
  • 20
  • 112
  • 171
  • 2
    Might I suggest base 32 instead? if any human is ever going to have to transcribe this sequence, Crockford's Base32 is a far superior choice http://crockford.com/wrmg/base32.html – Breton Jan 22 '09 at 00:06
3

You can use whatever, even a plain random number generator; however, you should check that the reservation code isn't already present. If this is the case, add characters ('x') to the string (date+user) until you get a new random/sha1/etc.

I'm only looking to have ~500 reservations, so I don't imagine high likelyhood of collissions.

Another stupid idea: generate 1000 or 2000 unique random numbers with the desired properties, store them somewhere, and assign them to the users as they register :)

Federico A. Ramponi
  • 46,145
  • 29
  • 109
  • 133
2

Here's 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 a random # number between 0 and scalar @chars $random_string .= $chars[rand @chars]; } return $random_string . "-" . time(); }

I don't remember how long the time() part is, so you may have to adjust the numbers to fit your length. You can also remove that part if you don't need it.

Anders Sandvig
  • 20,720
  • 16
  • 59
  • 73
2

If it's really just 500, then pre-generate 20,000 of them, into a table, then get the "next unused one" when you need it.

Gregg Lind
  • 20,690
  • 15
  • 67
  • 81
1

Some good tips on this question: How do I create a random alpha-numeric string in C++?

I'd avoid including characters like "1", "l", and "O", "0" and "5", "S", and "Z", "2" in your string, to make it easier for customers when they need to read your reservation code over the phone. The algorithm presented at that link should help you do this.

Community
  • 1
  • 1
jm.
  • 23,422
  • 22
  • 79
  • 93
0

In C# you can use http://www.dotnetfunda.com/forums/thread1357-how-do-generate-unique-alpha-numeric-random-number-in-aspnet.aspx (the super easy way, they say)

Raja1
  • 11
  • 2
0

use a guid? 16 characters, though if you really don't care about collision, you could just choose the first n characters.

Daniel
  • 1,516
  • 1
  • 13
  • 24
  • Note that substrings of GUIDs are not guaranteed to be at all unique http://blogs.msdn.com/oldnewthing/archive/2008/06/27/8659071.aspx – Eclipse Jan 21 '09 at 23:56
  • The guid algorithm is guaranteeing uniqueness only, you can't assume the generated guids to be random which would be a requirement for the use as a session id. – Dirk Vollmar Jan 22 '09 at 00:19
  • see also http://stackoverflow.com/questions/467271/how-random-is-system-guid-newguid#467296 – Dirk Vollmar Jan 22 '09 at 00:21