By default,. Net random numbers are generated based on the system time. If the computer speed is very fast, the random numbers will be the same.
Random RND = new random ();
Int rndnum = RND. Next (); // random number within the int value range
Int rndnum = RND. Next (10); // get 0 ~ Random Number of 9
Int rndnum = RND. Next (10, 20); // get 10 ~ Random Number of 19
Int rndnum = RND. nextdouble (); // 0 ~ Random Number of 1
If the random seed is the system time, multiple random numbers are generated at a time in a loop.
Because the CPU operation speed is too fast, every time you get the same time, the generated numbers are all the same.
So we need to constantly change the seeds.Public String getrandomcode ()
{
Char [] chars = {
'A', 'B', 'C', 'D', 'E', 'E', 'F', 'G', 'h', 'J', 'k ', 'M', 'n', 'P', 'Q', 'R','s ',
'T', 'U', 'V', 'w', 'x', 'y', 'z', '2', '3', '4 ', '5', '6', '7', '8', '9'
};
String code = string. empty;
For (INT I = 0; I <4; I ++)
{
// The key here is to pass in a seed parameter to ensure that the generated random number is different.
// Random RND = new random (unchecked (INT) datetime. Now. ticks ));
Random RND = new
Random (getrandomseed ());
Code + = chars [RND. Next (0, 30)]. tostring ();
}
Return code;
}
/// <Summary>
/// Encrypt the random number generator to generate a Random Seed
/// </Summary>
/// <Returns> </returns>
Staticint getrandomseed ()
{
Byte [] bytes =
Newbyte [4];
System. Security. cryptography. rngcryptoserviceprovider RNG = new system. Security. cryptography. rngcryptoserviceprovider ();
RNG. getbytes (bytes );
Returnbitconverter. toint32 (bytes, 0 );
}
Get the idea of shuffling a specified number of random combinations
Public static ilist <string> createchargecodeno (string promotionchargecodeno, int count)
{
List <string> Lis = new list <string> ();
If (string. isnullorempty (promotionchargecodeno ))
{
Return Lis;
}
String chargecodeno = promotionchargecodeno;
Int length = 10-promotionchargecodeno. length;
While (LIS. Count <count)
{
Int [] numbers = new int [length * 2];
For (INT I = 0; I <length * 2; I ++)
Numbers [I] = I + 1;
For (INT I = 0; I <length * 2; I ++) // shuffled
{
Random Rand = new random (getrandomseed ());
Int temp = Rand. Next (length * 2 );
Int tempnumber = numbers [I];
Numbers [I] = numbers [temp];
Numbers [temp] = tempnumber;
}
String code = "";
For (INT x = 0; Code. Length <length; X ++)
{
Code + = numbers [x];
}
Code = code. substring (0, length );
String S = chargecodeno + code;
If (LIS. Contains (s ))
{
Continue;
}
Lis. Add (chargecodeno + Code );
}
Return Lis;
}