2
我在for-next-statement中創建3個隨機字節。For-Next-Statement中的新隨機數重複相同的值
但是,它們對於每個for-next都是相同的,例如「333」或「777」。
我想知道爲什麼這樣,我做錯了什麼。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim s As String = String.Empty
For i As Integer = 0 To 2
s += GetRandomByte().ToString
Next
Me.TextBox1.Text = s
End Sub
Public Function GetRandomByte(Optional ByVal uLow As Byte = 1, Optional ByVal uHigh As Byte = 9) As Byte
Dim r As New Random
Dim b As Integer = r.Next(CInt(uLow), CInt(uHigh + 1))
Return b
End Function
更好的辦法是在方法外部聲明Random實例。 –