Vb.net Random Number generator generating same number many times

后端 未结 1 1515
执念已碎
执念已碎 2020-11-27 08:13

I have this program generating a firstname and lastname from a file. When I run this program I enter information in three textboxes. The first two are the salary low and h

相关标签:
1条回答
  • 2020-11-27 08:56

    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
    
    0 讨论(0)
提交回复
热议问题