Call to Rnd() generating the same number

前端 未结 3 1593
感动是毒
感动是毒 2020-12-06 18:51

When I set up this bit of code, every time I debug the software it generates the same number. Can anyone tell me why this is happening?

dim value as integer
         


        
3条回答
  •  情书的邮戳
    2020-12-06 19:06

    Since this was tagged as .Net you should not be using VB6 legacy functions. For .Net use the Random Class Here is an example that requires a button and a label.

    Public Class Form1
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Label1.Text = GenRandInt(100).ToString
        End Sub
    
        Private Shared prng As New Random 'only need one - should not be in a method
    
        Private Function GenRandInt(maxValue As Integer) As Integer
            'returns an integer between 0 and maxValue inclusive
            Return prng.Next(maxValue + 1)
        End Function
    End Class
    

    Read the documentation for the details and other uses of the Random class.

    edit: If for some reason you decide to continue to use Rnd, then call Randomize before using it.

提交回复
热议问题