Invite Code algorithm

Source: Internet
Author: User

Website to do an invitation code to register the function of their own on the internet to find a lot of generate 6-bit invitation code algorithm, but not a particularly satisfied, always feel not particularly perfect. Oneself on the combination of an algorithm on the internet to improve a bit, but also look at the Master of experience guidance. I am also a novice programmer, write down to record their growth in the learning process!!!

public string Getinvitationcode ()

{

Custom binary with a length of 34. 0 and 1 are easily confused with O and L and are not included in the binary.

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;//Generate six-bit invitation code

int binlen = R.length;

int charpos = 33;

With 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));

Automatic random completion when the length is not enough for 6 bits

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 ();

}

Invite Code algorithm

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.