Generate serial number using letters and digits

2.7k Views Asked by At

I'm developing an application for taking orders in C# and DevExpress, and I need a function that generates a unique order number. The order number must contain letters and digits and has a length of 20 .. I've seen things like Guid.NewGuid() but I don't want it to be totally random, nor to be just an auto increment number ..

Can anyone help? even if it's a script in a different language, I need ideas desperately :)

6

There are 6 best solutions below

0
On

My two cents.

If you need ideas then take a look at the Luhn and Luhn mod N algorithms.

While these algorithms are not unique code generators, they may give you some ideas on how to generate codes that can be validated (such that you could validate the code for correctness before sending it off to the database).

1
On

Two different methods:

  • Create MD5 or SHA1 hash of current time
  • Hash of increment number
0
On

Like Oded suggested, Guid is not random (well, not if you have a network card). It's based on time and location coordinates. See Raymond Chens blog post for a detailed explanation.
You are best off using an auto incremented int for order ids. I don't understand why you wouldn't want to use it or failing that a Guid?

I can't think of any way other then an auto id to maintain uniqueness and represent the order of your different orders in your system.

0
On

You can create type of your own . lets say yyyyMMddWWW-YYY-XXXXXXX where WWW is the store number, YYY the cashier id XXXXXXX is a hexadecimal number ( -> maybe an actual autoincrement number that you turn it into hex ) . This is just an idea . Im afraid you have to decide by the elements of your system how it will be .

edited : also if you can apply a check digit algorithm on it will also help in avoiding mistakes

0
On

One thought comes to mind.
Take the DateTime.Now.Ticks convert it to hexadecimal string.
Voila, String.Format("{0:X}", value);
If not long enough , you said you need 20 digits, you can always pad with zeros.

0
On
  1. Get the mother board ID
  2. Get the hdd ID
  3. Merge it by any way
  4. Add your secret code
  5. Apply MD5
  6. Apply Base54

Result: the serial code which is linked to the currect client PC :)