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