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