Copy Code code as follows:
<summary>
return random number
</summary>
<param name= "Vcodenum" ></param>
<returns></returns>
public string rndnum (int vcodenum)
{
String Vchar = "0,1,2,3,4,5,6,7,8,9";
string[] Vcarray = Vchar.split (', ');
String vnum = "";//because the strings are very short, you don't have to StringBuilder.
int temp = -1;//record last random value, try to avoid producing several random numbers
A simple algorithm is used to guarantee the difference of generating random numbers
Random rand = new Random ();
for (int i = 1; i < Vcodenum + 1; i++)
{
if (temp!=-1)
{
Rand = new Random (i * temp * unchecked ((int) DateTime.Now.Ticks));
}
int t = rand. Next (35);
int t = rand. Next (9);
if (temp!=-1 && temp = t)
{
Return Rndnum (Vcodenum);
}
temp = t;
Vnum + = vcarray[t];
}
return vnum;
}
Only String code = Rndnum (4) is required for invocation;
This results in 4-bit random numbers.
If you need a random number of letters,
String Vchar = "0,1,2,3,4,5,6,7,8,9,a,b"; Add the letters.
also modify int t = rand. Next (9); The 9 in the Vchar is changed to the length but can be.