J. Scott Elblein
Well-known member
Hello all
I have a basic function that is supposed to return a random number, between a low and high number you pass as parameters. I am trying to add a seed value, so that the same number doesn't happen twice in a row.
Currently, I have a weird problem with it, It is returning a 0 as the random number, very often, even though I pass the numbers 1 and 4 as the low, high, respectively.
Here is the code as I have it so far. I am a VB.NET beginner, so any and all help is appreciated.
TIA
I have a basic function that is supposed to return a random number, between a low and high number you pass as parameters. I am trying to add a seed value, so that the same number doesn't happen twice in a row.
Currently, I have a weird problem with it, It is returning a 0 as the random number, very often, even though I pass the numbers 1 and 4 as the low, high, respectively.
Here is the code as I have it so far. I am a VB.NET beginner, so any and all help is appreciated.
TIA
VB.NET:
Public Function RandomNumber(ByVal low As Int32, ByVal high As Int32) As Integer
' This function simply generates a random integer using the sent parameters.
' Example usage: RandomNumber(1,3)
Static RandomNumGen As New System.Random
' We need a seed value so we don't get the same 'random' number two times in a row
Static SeedValue As Int32
Dim intNum As Int32
Do Until Not intNum = SeedValue
intNum = RandomNumGen.Next(low, high + 1)
Loop
Debug.Print("Seed: " & SeedValue.ToString)
' Update the current random # to the Seed
SeedValue = intNum
Debug.Print("New: " & SeedValue.ToString)
Return intNum
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim intTest As Int32
intTest = RandomNumber(1, 4)
End Sub