目前我的程序(见下文)生成长度为8-12个字符的随机字符串(由数字组成).
public static string GenerateNewCode(int CodeLength)
{
string newCode = String.Empty;
int seed = unchecked(DateTime.Now.Ticks.GetHashCode());
Random random = new Random(seed);
// keep going until we find a unique code `
do
{
newCode = random.Next(Convert.ToInt32(Math.Pow(10, CodeLength - 1)), Convert.ToInt32(Math.Pow(10, CodeLength) - 1)).ToString("0000");
}
while (!ConsumerCode.isUnique(newCode));
// return
return newCode;
}
Run Code Online (Sandbox Code Playgroud)
然而,它们是该方法的问题,当其codeLength为10或更大时,它会导致错误,因为10 9大于int32.MaxValue.
不知道如何解决这个问题.