Currently my program (see below) generates random strings (made of numbers) from 8 – 12 characters in length.
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;
}
However, their is a problem with the method, when the codeLength is 10 or greater it causes error because 109 is greater than int32.MaxValue.
Not sure how to get around this issue.
Your code doing the same thing without some freak lines: