Best way to generate order numbers for an online store?

At my old place it was the following:

The customer ID (which started at 1001), the sequence of the order they made then the unique ID from the Orders table. That gave us a nice long number of at least 6 digits and it was unique because of the two primary keys.

I suppose if you put dashes or spaces in you could even get us a little insight into the customer's purchasing habits. It isn't mind boggling secure and I guess a order ID would be guessable but I am not sure if there is security risk in that or not.


Here is an implementation for a system I proposed in an earlier question:

MAGIC = [];
29.downto(0) {|i| MAGIC << 839712541[i]}

def convert(num)
  order = 0
  0.upto(MAGIC.length - 1)  {|i| order = order << 1 | (num[i] ^ MAGIC[i]) }
  order
end

It's just a cheap hash function, but it makes it difficult for an average user to determine how many orders have been processed, or a number for another order. It won't run out of space until you've done 230 orders, which you won't hit any time soon.

Here are the results of convert(x) for 1 through 10:

1:  302841629
2:  571277085
3:   34406173
4:  973930269
5:  437059357
6:  705494813
7:  168623901
8:  906821405
9:  369950493
10: 638385949

As a customer I would be happy with:

year-month-day/short_uid

for example:

2009-07-27/KT1E

It gives room for about 33^4 ~ 1mln orders a day.


Ok, how about this one?

Sequentially, starting at some number (2468) and add some other number to it, say the day of the month that the order was placed.

The number always increases (until you exceed the capacity of the integer type, but by then you probably don't care, as you will be incredibly successful and will be sipping margaritas in some far-off island paradise). It's simple enough to implement, and it mixes things up enough to throw off any guessing as to how many orders you have.