Vb.net Random Number generator generating same number many times

You’re using a new instance of System.Random every time. Random is seeded by the current time.

Initializes a new instance of the Random class, using a time-dependent default seed value

Reference

Since you are creating new instances in very quick succession, they get the same seed.

Instead, you should use the same instance of Random, possibly by making it a field and initializing as a field initializer or constructor. For example:

Public Class Form1
    Private _random As New System.Random()

    'Use _random in other methods.
End Class

Leave a Comment