lundi 1 mai 2017

Does the presence of duplicate random numbers here prove that C#'s random generator number isn't random enough?

This question (and its self-answer) was inspired by this question. Please go easy on me, this is the first time I've done this kind of question and immediate self-answer :)

I wrote a program to generate random words:

private static string GenerateRandomWord(Random r, int length)
{
    char[] letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();

    StringBuilder word = new StringBuilder();

    for (int i = 0; i < length; i++)
    {
        int index = r.Next(0, 25);

        word.Append(letters[index]);
    }

    return word.ToString();
}

static void Main(string[] args)
{
    Random r = new Random();
    for (int i = 0; i < 50; i++)
    {
        // Generate 50 "words" consisting of 4 random letters
        Console.WriteLine(GenerateRandomWord(r, 4));
    }
}

However, I'll periodically get words like WXJX where the same letter appears multiple times in the same word or even words like TTFD and WWOB where the same letter appears twice in a row. Is this evidence that C#'s Random class isn't actually very random?




Aucun commentaire:

Enregistrer un commentaire