Today, I sorted out the source code of a previous project and found two methods to randomly generate non-repeated strings. I think it is very convenient to use them. I will post them here for future reference. Method 1: generate a random string without repeated numbers
PrivateIntRep= 0;
///
/// Generate random numeric string
///
/// Number of digits to be generated
/// Generated numeric string
Private String Generatecheckcodenum (Int Codecount)
{
String Str = String. empty;
Long Num2 = Datetime. Now. ticks + This. REP;
This. Rep ++;
Random Random = New Random (((INT) (ulong) num2) & 0 xffffffffl )) | ((INT) (num2 > This. REP )));
For (Int I = 0; I < Codecount; I++)
{
Int Num = Random. Next ();
Str = Str + ((Char) (0x30 + ((Ushort) (Num % 10). tostring ();
}
Return STR;
}
Code Method 2: generate a random string (mixed digits and letters)
///
///Generate random character strings (mixed numbers and letters)
///
///Number of digits to be generated
///Generated letter string
PrivateStringGeneratecheckcode (IntCodecount)
{
StringStr=String. empty;
LongNum2=Datetime. Now. ticks+This. REP;
This. Rep ++;
RandomRandom=NewRandom (((INT) (ulong) num2)&0 xffffffffl ))|((INT) (num2>This. REP )));
For(IntI=0;I<Codecount;I++)
{
CharCh;
IntNum=Random. Next ();
If(Num%2)=0)
{
Ch=(Char) (0x30+((Ushort) (Num%10 )));
}
Else
{
Ch=(Char) (0x41+((Ushort) (Num%0x1a )));
}
Str=Str+Ch. tostring ();
}
ReturnSTR;
}