First, the order number has three properties: 1. uniqueness 2. Non-Testability 3. Efficiency
Uniqueness and unpredictability needless to say, Efficiency refers to the inability to frequently query databases to avoid duplication.
Besides, the order number must be short enough when these conditions are met.
The custom order number generation method in Java is as follows:
Int R1 = (INT) (math. Random () * (10); // generates two random numbers 0-9.
Int r2 = (INT) (math. Random () * (10 ));
Long Now = system. currenttimemillis (); // a 13-bit Timestamp
String paymentid = string. valueof (R1) + String. valueof (R2) + String. valueof (now); // order ID
According to the current rules, the probability of two people submitting orders in the same microsecond is 1%
Order Effect: 251393292999921
C:
Random r = new random (); For (INT I = 0; I <20; I ++) {int R1 = (INT) (R. next (1, 9) * (10); // generate two 0-9 random numbers int r2 = (INT) (R. next (1, 9) * (10); datetime tmptime = datetime. now; Long Now = long. parse (tmptime. tostring ("yymmddhhmmss") + "99" + tmptime. millisecond. tostring (); // a 13-bit timestamp string paymentid = convert. tostring (R1) + "_" + convert. tostring (R2) + "_" + convert. tostring (now); // order ID textbox2.appendtext (paymentid + "\ r \ n ");}
There is another nature that cannot be too long. The date and time should not be explicitly displayed. Converting to int will save one or two digits.