A good algorithm for generating an order number

前端 未结 10 1448
青春惊慌失措
青春惊慌失措 2021-01-31 06:27

As much as I like using GUIDs as the unique identifiers in my system, it is not very user-friendly for fields like an order number where a customer may have to repeat that to a

相关标签:
10条回答
  • 2021-01-31 06:53

    Maybe you could try generating some unique text using a markov chain - see here for an example implementation in Python. Maybe use sequential numbers (rather than random ones) to generate the chain, so that (hopefully) the each order number is unique.

    Just a warning, though - see here for what can possibly happen if you aren't careful with your settings.

    0 讨论(0)
  • 2021-01-31 06:55

    If the middle tier cannot check what "order numbers" already exists in the database, the best it can do will be the equivalent of generating a random number. However, if you generate a random number that's constrained to be less than 1 billion, you should start worrying about accidental collisions at around sqrt(1 billion), i.e., after a few tens of thousand entries generated this way, the risk of collisions is material. What if the order number is sequential but in a disguised way, i.e. the next multiple of some large prime number modulo 1 billion -- would that meet your requirements?

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

    We do TTT-CCCCCC-1A-N1.

    • T = Circuit type (D1E=DS1 EEL, D1U=DS1 UNE, etc.)
    • C = 6 Digit Customer ID
    • 1 = The customer's first location
    • A = The first circuit (A=1, B=2, etc) at this location
    • N = Order type (N=New, X=Disconnect, etc)
    • 1 = The first order of this kind for this circuit
    0 讨论(0)
  • 2021-01-31 07:02

    Your 10 digit requirement is a huge limitation. Consider a two stage approach.

    1. Use a GUID
    2. Prefix the GUID with a 10 digit (or 5 or 4 digit) hash of the GUID.

    You will have multiple hits on the hash value. But not that many. The customer service people will very easily be able to figure out which order is in question based on additional information from the customer.

    0 讨论(0)
  • 2021-01-31 07:02

    The straightforward answer to most of your bullet points:

    Make the first six digits a sequentially-increasing field, and append three digits of hash to the end. Or seven and two, or eight and one, depending on how many orders you envision having to support.

    However, you'll still have to call a function on the back-end to reserve a new order number; otherwise, it's impossible to guarantee a non-collision, since there are so few digits.

    0 讨论(0)
  • 2021-01-31 07:03

    Use primitive polynomials as finite field generator.

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