NadaNK
NadaNK

Reputation: 802

Generate serial number using letters and digits

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 :)

Upvotes: 0

Views: 2834

Answers (6)

James Hull
James Hull

Reputation: 3689

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.

Upvotes: 0

SAK
SAK

Reputation: 470

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).

Upvotes: 0

Viacheslav Smityukh
Viacheslav Smityukh

Reputation: 5833

  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 :)

Upvotes: 0

Erez Robinson
Erez Robinson

Reputation: 784

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.

Upvotes: 0

isioutis
isioutis

Reputation: 324

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

Upvotes: 1

Martin Risell Lilja
Martin Risell Lilja

Reputation: 652

Two different methods:

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

Upvotes: 0

Related Questions