samedi 20 janvier 2018

Why isn't Random() being regenerated inside while loop? C# [duplicate]

I'm trying to fill the screen with random chars, the problem is it's not always unique, but it's also not always the same on one line. I'm confused whether I need a 3rd while loop or am missing something else?

int w = Console.WindowWidth;
int h = Console.WindowHeight;
string[,] matrix = new string[w, h];
int i = 0;
while (i < w)
{
    int j = 0;
    while (j < h)
    {
        string possible = "0123456789";
        var random = new Random();
        var n = random.Next(0, possible.Length);
        matrix[i, j] = possible[n].ToString();
        Console.Write(matrix[i, j]);
        j++;
    }
    i++;
}




Aucun commentaire:

Enregistrer un commentaire