Java generate random non-repeated promotion code invitation code

Source: Internet
Author: User

Welcome to my blog: blog.scarlettbai.com view many other articles

Recently received a demand. To batch generate promotional code, we first know that the promotional code of special effects such as the following two points:

1: Do not repeat

2: Can not be guessed

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

1: Every time a random code is generated to check whether the database has the same , there is another generation (each time to access the database.) result 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 bits (according to demand or increase) random numbers or letters. Insert the primary key value. Make up a string such as 8000e0v0s1d, can ensure that the promotion code is not repeated and cannot be guessed (this way also need to connect the database to primary key, of course, can pre-generate the required number of primary keys, and then generate the corresponding promotion code updated into the database, the efficiency will be higher)

3: Use 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)

Detailed 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 detailed effect:


Can be seen in the figure. the random string generated by the UUID is removed-the length is 32 bits , although it ensures that almost no repetition is possible and supports multithreading concurrency without repetition. But the length is too long , not very useful.

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 is not much descriptive narrative. We are interested in being able to Google Baidu, algorithm online also has a large pile, we can casually next on the good. The ciphertext here is the self-increasing number. Secret key write Dead , next we look at the effect:


Can be seen in the figure. The generated code is not in the letter and 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 to 16, the effect such as the following:


We can see, at this time the ciphertext has become familiar with the letters and numbers, but the length of the original character is twice times the length , but still can accept, but look at the encryption after the cipher is continuous, not meet the promo code 2nd can be guessed, and because the private key is all the same easy to be cracked, Here we do not need to decrypt. So you can change the private keydirectly to the UUID to see the effect:


Can see. The ciphertext generated here can no longer be guessed, satisfying our needs. So, whether it will be repeated. Let's do a test again. The required encrypted string length is 7 bits :


It can be seen that when the string length is 7 bits, the Covin ciphertext is generated. are not repeated , basically can meet most of the situation, many other I did not test. It is interesting to be able to test how many bits are generated when they occur repeatedly. The advantage of this approach is that. The longer the original string length. Generated will not repeat the level of ciphertext will be larger, we can adjust on demand.

The only disadvantage of this way is that. The resulting ciphertext length is even , assuming that we need to determine the odd length of ciphertext, can be RC4 encryption results in other ways to convert to our regular use of letters and numbers, the landlord of this place is no special needs. Therefore, the direct use of the conversion of 16 binary such a fast lazy way.

Welcome to pay attention to the individual public number: Reading fitness programming

Java generate random non-repeated promotion code invitation code

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.