Invitation code algorithm, invitation Algorithm

Source: Internet
Author: User

Invitation code algorithm, invitation Algorithm

The website wants to register an invitation code. I have found many algorithms on the Internet to generate 6-digit invitation codes, but none of them are very satisfied. I always think they are not perfect. I have made some improvements based on an algorithm on the Internet. I also hope you will be guided by experienced masters. I am also a beginner in programming. I wrote it down to record my learning process as I grew up !!!

Public string GetInvitationCode ()

{

// Custom hexadecimal notation with a length of 34. 0 and 1 are easy to confuse with o and l, and are not included in the system.

Char [] r = new char [] {'Q', 'w', 'E', '8', 'A', 's', '2 ', 'D', 'z', 'x', '9', 'C', '7', 'P', 'O', '5', 'I ', 'k', '3', 'M', 'J', 'U', 'F', 'R', '4', 'V', 'y ', 'l', 't', 'n', '6', 'B', 'G', 'H '};

Char [] B = new char [] {'Q', 'w', 'E', '5', 'A', 's', '3 ', 'D', 'z', 'x', '8', 'C', '2', 'P', 'O', '4', 'I ', 'k', '9', 'M', 'J', 'U', 'F', 'R', '6', 'V', 'y ', 'T', 'n', '7', 'B', 'G', 'H '};

Char [] buf = new char [33];

Int s = 6; // generates a six-digit invitation code.

Int binLen = r. Length;

Int charPos = 33;

// Use the current number of milliseconds as the standard

Int id = DateTime. Now. Millisecond;

While (id/binLen> 0)

{

Int k = (int) (id % binLen );

Buf [-- charPos] = r [k];

Id/= binLen;

}

Buf [-- charPos] = r [(int) (id % binLen)];

String str = new String (buf, charPos, (33-charPos ));

// If the length is not 6 characters long, the system automatically performs random completion.

If (str. Length <s)

{

StringBuilder sb = new StringBuilder ();

Random rd = new Random ();

For (int I = 1; I <= s-str. Length; I ++)

{

Sb. Append (B [rd. Next (33)]);

}

Str + = sb. ToString ();

}

Return str. ToUpper ();

}

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.