My ASP.NET application requires me to generate a huge number of random strings such that each contain at least 1 alphabetic and numeric character and should be alphanumeric on the whole. For this my logic is to generate the code again if the random string is numeric:
        public static string GenerateCode(int length)
        {
            if (length < 2 || length > 32)
            {
                throw new RSGException("Length cannot be less than 2 or greater than 32.");
            }
            string newcode = Guid.NewGuid().ToString("n").Substring(0, length).ToUpper();
            return newcode;
        }
        public static string GenerateNonNumericCode(int length)
        {
            string newcode = string.Empty;
            try
            {
                newcode = GenerateCode(length);
            }
            catch (Exception)
            {
                throw;
            }
            while (IsNumeric(newcode))
            {
                return GenerateNonNumericCode(length);
            }
            return newcode;
        }
        public static bool IsNumeric(string str)
        {
            bool isNumeric = false;
            try
            {
                long number = Convert.ToInt64(str);
                isNumeric = true;
            }
            catch (Exception)
            {
                isNumeric = false;
            }
            return isNumeric;
        }
While debugging, it is working properly but when I ask it to create 10,000 random strings, its not able to handle it properly. When I export that data to Excel, I find at least 20 strings on an average that are numeric.
Is it a problem with my code or C#? - Mine.
If anyone's looking for code,
public static string GenerateCode(int length)
    {
        if (length < 2)
        {
            throw new A1Exception("Length cannot be less than 2.");
        }
        var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        var random = new Random();
        var result = new string(
        Enumerable.Repeat(chars, length)
                  .Select(s => s[random.Next(s.Length)])
                  .ToArray());
    return result;
}
public static string GenerateAlphaNumericCode(int length)
{
    string newcode = string.Empty;
    try
    {
        newcode = GenerateCode(length);
        while (!IsAlphaNumeric(newcode))
        {
            newcode = GenerateCode(length);
        }
    }
    catch (Exception)
    {
        throw;
    }
    return newcode;
}
public static bool IsAlphaNumeric(string str)
{
    bool isAlphaNumeric = false;
    Regex reg = new Regex("[0-9A-Z]+");
    isAlphaNumeric = reg.IsMatch(str);
    return isAlphaNumeric;
}
Thanks to all for your ideas.
 
     
     
     
     
     
    