mercredi 30 janvier 2019

Random number generation seemingly not working properly

There's a thing that's happening that I don't understand the reason behind.

In Visual Basic, I've got a 2D Integer array that I populate with random numbers. When I do it with Rnd():

For j = 0 To matrix.GetUpperBound(0)
  For i = 0 To matrix.GetUpperBound(1)
    matrix(j, i) = 1 + CType((Rnd() * 50), Integer)
  Next
Next

It always writes the same numbers in the same position in the matrix.

But when I do it with the Random class:

Dim r As Random = New Random()
For j = 0 To matrica.GetUpperBound(0)
  For i = 0 To matrica.GetUpperBound(1)
    matrica(j, i) = r.Next(0, 100)
  Next
Next

It does what it's supposed to.

I don't understand why in the first case I get the same numbers in the same position every time I run the code.




Aucun commentaire:

Enregistrer un commentaire