lundi 18 janvier 2016

Random is not working good

I generate a string with 62 options ^ 6 letters = 56,800,235,584

But when running the code, it repeats the same string less then every 200,200 times

What is the problem here?

BTW: This code is based on the answer here

class Program
{
    static void Main(string[] args)
    {
        var d = new Dictionary<string, bool>();

        for (int i = 0; ; i++)
        {
            var s = GenerateString(6);
            try
            {
                d.Add(s, false);
            }
            catch (Exception ex)
            {
                Console.WriteLine(String.Format("{0} - {1} - {2}", i, s, ex.Message));
                i = 0;
            }
        }

        Console.ReadKey();
    }


    static Random _rnd = new Random();
    public static string GenerateString(int len)
    {
        const string bigLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        const string smallLetters = "abcdefghijklmnopqrstuvwxyz";
        const string numbers = "1234567890";

        var validChars = bigLetters + smallLetters + numbers;

        var result = new StringBuilder();

        for (int i = 0; i < len; i++)
        {
            result.Append(validChars[_rnd.Next(validChars.Length)]);
        }

        return result.ToString();
    }
}




Aucun commentaire:

Enregistrer un commentaire