Best way to generate order numbers for an online store?

前端 未结 13 2050
野的像风
野的像风 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:42

    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
    

提交回复
热议问题