Best way to generate order numbers for an online store?

前端 未结 13 2048
野的像风
野的像风 2021-01-31 00:09

Every order in my online store has a user-facing order number. I\'m wondering the best way to generate them. Criteria include:

  • Short
  • Easy to say over the
相关标签:
13条回答
  • 2021-01-31 00:29

    Something like this:

    1. Get sequential order number. Or, maybe, an UNIX timestamp plus two or three random digits (when two orders are placed at the same moment) is fine too.
    2. Bitwise-XOR it with some semi-secret value to make number appear "pseudo-random". This is primitive and won't stop those who really want to investigate how many orders you have, but for true "randomness" you need to keep a (large) permutation table. Or you'll need to have large random numbers, so you won't be hit by the birthday paradox.
    3. Add checkdigit using Verhoeff algorithm (I'm not sure it will have such a good properties for base33, but it shouldn't be bad).
    4. Convert the number to - for example - base 33 ("0-9A-Z", except for "O", "Q" and "L" which can be mistaken with "0" and "1") or something like that. Ease of pronouncation means excluding more letters.
    5. Group the result in some visually readable pattern, like XXX-XXX-XX, so users won't have to track the position with their fingers or mouse pointers.
    0 讨论(0)
  • 2021-01-31 00:32

    Just one Rube Goldberg-style idea:

    You could generate a table with a random set of numbers that is tied to a random period of time:

    Time Period          Interleaver
    next 2 weeks:        442
    following 8 days:    142
    following 3 weeks:   580
    

    and so on... this gives you an unlimited number of Interleavers, and doesn't let anyone know your rate of orders because your time periods could be on the order of days and your interleaver is doing a lot of low-tech "mashing" for you.

    You can generate this table once, and simply ensure that all Interleavers are unique. You can ensure you don't run out of Interleavers by simply adding more characters into the set, or start by defining longer Interleavers.

    So you generate an order ID by getting a sequential number, and using today's Interleaver value, interleave its digits (hence, the name) in between each sequential number's digits. Guaranteed unique - guaranteed confusing.

    Example:

    Today I have a sequential number 1, so I will generate the order ID:  4412
    The next order will be 4422
    The next order will be 4432
    The 10th order will be 41402
    
    In two weeks my interleaver will change to 142, 
    The 200th order will be 210402
    The 201th order will be 210412
    
    Eight days later, my interleaver changes to 580:
    The 292th order will be 259820
    

    This will be completely confusing but completely deterministic. You can just remove every other digit starting at the 1's place. (except when your order id is only one digit longer than your interleaver)

    I didn't say this was the best way - just a Friday idea.

    0 讨论(0)
  • 2021-01-31 00:32

    How about getting the current time in miliseconds and using that as your order ID?

    0 讨论(0)
  • 2021-01-31 00:33

    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.

    0 讨论(0)
  • 2021-01-31 00:39

    http://blog.logeek.fr/2009/7/2/creating-small-unique-tokens-in-ruby

    >> rand(36**8).to_s(36)
    => "uur0cj2h"
    
    0 讨论(0)
  • 2021-01-31 00:41

    Sequentially, starting at 1? What's wrong with that?

    (Note: This answer was given before the OP edited the question.)

    0 讨论(0)
提交回复
热议问题