Help with a randon number generator

J. Scott Elblein

Well-known member
Joined
Dec 14, 2006
Messages
166
Location
Chicago
Programming Experience
10+
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 :)

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
 
Your entire method is pointless. Just declare a member variable and assign it a new Random object, then call the Next method of that object each time you need a number.
 
You are totally misinformed about the "seed" value.

A seed value is used if you want to duplicate the same series or sequence of random numbers, not the other way around.

The key word here is the series/sequence of numbers. Each time you generate a new random number from a range, it starts all over again and can select any one of those numbers, including the same number, which has been tossed back into the range. You have no control over that.

Without a specified seed number, the default seed used is the system timer, which changes several times every second, making it unpredictable each time you start a new sequence. If you use a specified seed number, the series will restart always from the same number, generating the same sequence each time. However, as I stated above, the random number may be duplicated either way.

I think what you are looking for is a sequence of numbers with no duplicates. That requires the use of an array, which must then be shuffled using a special sorting algorithm.

Your function needs to be completely redone to include a shuffled array. Other than that, if you simply want another random number that doesn't duplicate the previous one, then you need to compare the current random number with the previous one and select a new number if they are the same. However, this won't prevent the same number from showing up again later.
 
Thank you both for your replies. :)

Your entire method is pointless. Just declare a member variable and assign it a new Random object, then call the Next method of that object each time you need a number.

But isn't that what I did using this?:
Static RandomNumGen As New System.Random

and this?:
intNum = RandomNumGen.Next(low, high + 1)

So why is it returning a 0 so often, when I set the low as 1 and the high as 4?

Solitaire said:
Other than that, if you simply want another random number that doesn't duplicate the previous one, then you need to compare the current random number with the previous one and select a new number if they are the same. However, this won't prevent the same number from showing up again later.

Yes, that is all I want at the moment. What I was attempting to do in that function, was to save the "SeedValue" (although the term "Seed" is wrong I spose:)) across calls to the functon, which stores the previously saved number generated, and then use this loop:

Do Until Not intNum = SeedValue

So that it would keep generating a random number until it did not match the previous one in the "Seed". Once it generated a fresh number, I'd save it in the Seed for comparison again next time. (It gets called very frequently).

Sorry, if you guys already answered it properly and I'm just not getting it. :)
 
Ok, I found the problem. I guess I was just tired form too many hours of coding that day. :)

I just needed to change the loop from:

VB.NET:
        Do Until (Not intNum = SeedValue)
            intNum = RandomNumGen.Next(low, high + 1)
        Loop

To:

VB.NET:
        Do
            intNum = RandomNumGen.Next(low, high + 1)
        Loop Until (Not intNum = SeedValue)

Since the setting of the intNum var defaults to 0, the first comparison of the loop automatically forced it to bypass generating a new number.

Doh

I'm surprised you guys didn't catch that right away too! LOL :D

Anyway, thanks again for the replies.
 
Back
Top