Java generate random non-repeating promo code invitation code

Source: Internet
Author: User
Tags uuid

Welcome to my blog: blog.scarlettbai.com View more articles

Recently received a demand, to batch generate promotional code, first we know that the promotional code effects have the following two points:

1: Non-repeatable

2: Can not be pushed to measure

About these two points, our ideas are broadly divided into the following categories:

1: After each generation of a random code to check whether the database has the same , there is regeneration (every time to access the database, resulting in very low efficiency, not recommended)

2: According to the primary key of the database as a unique key , to disrupt or insert operations, such as the primary key is 8000001, after the removal of 3-bit (according to demand or increase) random number or letter, insert the primary key value, constitute such as 8000e0v0s1d such a string, can guarantee the promotion code is not duplicated and cannot be inferred ( This way also need to connect to the database primary key, of course, can pre-generate the required number of primary keys, and then generate corresponding promotion code updated into the database, the efficiency will be higher)

3: The use of the algorithm to ensure that the value is unique, such as UUID, this article focuses on this approach (do not need to connect to the database, the algorithm is very efficient selection of appropriate)

Specific implementation

Above said, this article focuses on the use of algorithms to achieve non-repeatability, first we will think of the simplest way:UUID, we look at the specific effect:


As can be seen in the figure, theuuid generated random string removal-after the length of 32 bits , although it can be guaranteed almost absolutely no duplication and support multi-threaded concurrency is not repeated, but the length is too long , not very practical.

Now that the UUID is eliminated, let's take a look at the SHA algorithm that we most often contact:


It can be seen that the string generated by the SHA algorithm is also significantly too long and obsolete .

Next I think of the symmetric encryption algorithm in the RC4, he can ensure that the length of the ciphertext and the original string length, about the RC4 algorithm This article does not describe, we are interested in Google Baidu, the algorithm online there are a lot, we can casually next on the good, here Secret papers for the self-increasing number, the key to write dead , next we look at the effect:


As can be seen in the figure, the generated code is not in the letter and the number range , it is not possible to take these codes to the user as a promotional code, then the simplest way is to convert this code into 16, the effect is as follows:


As you can see, the ciphertext is now known as the letters and numbers, but the length of the original character is twice times the length , but still can be accepted, but look at the encryption of the ciphertext is continuous, not meet the promotion code 2nd can be inferred, and because the private key is all the same easy to be cracked, Here we do not need to decrypt, so you can directly change the private key to UUID, to see the effect:


As you can see, the ciphertext generated here can no longer be extrapolated to meet our needs, so for the repetition, let's do another test, where the required length of the encrypted string is 7 bits :


As can be seen, the string length of 7 bits, the generation of Covin ciphertext, are not duplicated , the basic can meet most of the situation, more I did not test, we are interested to test the number of bits generated when the duplication occurs. The advantage of this approach is that the longer the original string length, the resulting volume of ciphertext that will not be duplicated will be larger, and you can adjust it as needed.

However, the only disadvantage of this way is that the resulting ciphertext length is even , if you need to determine the odd length of ciphertext, can be RC4 encryption results in other ways to convert to our usual letters and numbers, the landlord of this place is no special needs, So the direct use of the conversion 16 binary this fast lazy way.

Welcome to the individual public number:

Java generate random non-repeating promo code invitation code

Related Article

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.