Method analysis of generating unique random code

Source: Internet
Author: User

Now the web is often required to generate some invitation code, activation code. The need is unique and random. Here is a summary of some of the commonly used methods of generating random code, and share their own 1 methods:

1. Write your own code to generate a random combination of numbers and letters, each resulting in 1 go to the database query whether the random code already exists, if it already exists, then re-produce until it is not repeated.
Pros: No advantages found.
Disadvantage: Slow production, but also to query the database, when the amount of data is large, the probability of repetition may be higher, to query multiple databases.
2. Guid, this method should be used more.
Pros: Easy to use, no extra code to write yourself
Disadvantage: Occupy database space is relatively large, especially according to the GUID query speed is slow (after all, is a string).

3. The primary key + random code, we generate a random code saved to the database will definitely have a primary key, with the primary key + random characters combination. Production steps:
1) First get the ID from the ID generator, for example, 155.
2) A string filled with a fixed number of digits (for example, 8 bits) (the left side of the insufficient digits is 0, the number is used directly in the digits), and the resulting: 00000155
3) Randomly insert 1 letters or other non-numeric symbols after each number to get: 0a0f0r0y0h1k5l5m
This allows you to get 1 random, unique invitation codes.
Advantages: Use is also relatively simple, do not query the database. The biggest advantage is that when querying, you can get the primary key ID directly according to the invitation code.
Then according to the ID to the database query (FAST), and then compare the query out of the invitation code and the user submitted the invitation code is consistent.
Cons: Need to use ID generator, if the primary key is the database self-growth is not very good (need to insert the database to obtain the ID, and then update the invitation code).

4. Sometimes the product manager said that I asked for the number of the invitation code. Why No why? I like it. * (&^ (^%&^$&^$) Method 3 can be used to achieve the only pure digital random code.
1) Get id:155
2) Convert to 8 binary: 233
3) Convert to a string and add ' 9 ' characters after: 2339
4) randomly generate a number of random number characters at the back: 2003967524987
After converting to 8, the 9 character will not appear, and then add a ' 9 ', which will determine the uniqueness. Finally, a few random numbers are generated at the back.
Pros and cons with Method 3
At present method 3,4 method is used in our product, feel also can.

Http://www.cnblogs.com/BearsTaR/archive/2010/10/19/unique_random_code.html

Method analysis of generating unique random 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.