//Generate random numbers randomly from the strchar array in the function
//letters are case sensitive
//The parameter n is the number of digits to generate a random number, usually four digits
public string RandomNum(int n) //
{
string strchar = "0,1,2,3,4,5,6,7,8,9";
string[] VcArray = strchar.Split(',');
string VNum = "" ;//Since the string is very short, StringBuilder is not needed
int temp = -1; //Record the last random value and try to avoid generating several identical
random
numbers.
//Use a simple algorithm to ensure the difference in generated random numbers
Random rand =new Random();
for (int i = 1; i < n+1; i++)
{
if (temp != -1)
{
rand =new Random(i*temp*unchecked((int)
DateTime.Now.Ticks));
}
//int t = rand.Next(35);
int t=rand.Next(10);
if (temp != -1 && temp == t)
{
return RndNum(n);
}
temp = t;
VNum += VcArray[t];
}
return VNum;//Return the generated random number
}
After generating a random number, assign the value to a Label control, and then change the color of the Label background. OK