相关疑难解决方法(0)

随机数发生器 - 可变长度

目前我的程序(见下文)生成长度为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.

不知道如何解决这个问题.

c# random math integer numbers

2
推荐指数
1
解决办法
6622
查看次数

标签 统计

c# ×1

integer ×1

math ×1

numbers ×1

random ×1