How to generate a unique order number

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.