mercredi 7 août 2019

C# compiler and debuger gives different result for Random object [duplicate]

This question already has an answer here:

please could you explain why during complier we have randomly generated value - but the same for total array but during debugger it shows that for each element it's a randomly genereated value. For example:

static void Main(string[] args)
    {
        int[,] vs = new int[3, 4];
        //Random random = new Random();  // 1st example here random works ok in compiler
        for(int i = 0; i < vs.GetLength(0); i++)
        {
            for(int j = 0; j < vs.GetLength(1); j++)
            {
                Random random = new Random(); // 2nd example here value is random but the same for total array but during debugger it shows different result - each value in materix is randomly generated
                vs[i, j] = random.Next(100);
                Console.Write("{0,4}", vs[i, j]);
            }
            Console.WriteLine();
        }
        Console.ReadKey();
    }

I don't understand why, for example in first example it's:

  1. 12 43 2 51
  2. 3 5 1 4
  3. 12 99 42 12

In second example random place:

  1. 47 47 47 47
  2. 47 47 47 47
  3. 47 47 47 47

but if I use the debugger it shows me randomly generated each time like in the first exmaple? How to understand that?




Aucun commentaire:

Enregistrer un commentaire