random numbers

vbnet.care

New member
Joined
Nov 2, 2006
Messages
2
Programming Experience
Beginner
I am sure this is very simple, i am just new to vb.net and i can't figure this out. I am trying to generate random numbers from 0 to 10 and skip number 2. Is it possible? I can write the code to generate random numbers but I can't figure out how to skip a number. Help!
 
VB.NET:
Dim myRandom As New Random

'Get a random number from 0 to 9.
Dim myInteger As Integer = myRandom.Next(0, 10)

'Create a random number from 0 to 10 skipping 2
If myInteger > 1 Then
    myInteger += 1
End If
 
I would have expected it to be a bit more like this:

VB.NET:
Function RandWithSkip(Min as Integer, Max as Integer, Skip as Integer) as Integer
 
  Dim rnd as New Random
 
  Dim ret as Integer = rnd.Next(Min,Max)
 
  
  While(ret = Skip)
    ret = rnd.Next(Min,Max)
  End While
 
  Return ret
End FUnction

It took me a while to see that J's code achieves the same ends with a different means.. It's sure odd logic?! :)
 
There's no point generating a number and then throwing it away if it happens to be the number you want to skip. It wouldn't be a big deal in this case but let's say you wanted a to skip a lot more than just one number. You might have to throw away several attempts before getting one that's valid. Using my method every generated value is valid so you never have to throw anything away.
Thank you for your help. Could you please tell what the code mean?
if myinteger > 1 then my integer += 1
What that does is increment the value if it's greater than one. That way the numbers 0 to 1 stay as they are while the numbers 2 to 9 get shifted up one to become 3 to 10. That gives you the range 0 to 10 skipping 2. Here's a general method that works no matter how many values you want to skip:
VB.NET:
Private myRandom As New Random

Private Function GetRandomNumber(ByVal min As Integer, _
                                 ByVal max As Integer, _
                                 ByVal ParamArray skip() As Integer) As Integer
    'Make sure that the values to be skipped are in ascending order.
    Array.Sort(skip)

    'Generate a random number in a continuous sequence
    Dim result As Integer = Me.myRandom.Next(min, max + 1 - skip.Length)

    'Slide the value upwards to account for the gaps in the sequence.
    For index As Integer = 0 To skip.GetUpperBound(0) Step 1
        If result < skip(index) Then
            Exit For
        End If

        result += 1
    Next index

    Return result
End Function
Prove to yourself that it works by doing something like this:
VB.NET:
Dim min As Integer = 0
Dim max As Integer = 10
Dim skip As Integer() = {2, 5, 7}
Dim result(10) As Integer

For i As Integer = 1 To 1000
    result(Me.GetRandomNumber(min, max, skip)) += 1
Next i

Dim distribution As String = String.Empty

For i As Integer = 0 To result.GetUpperBound(0)
    distribution &= i & ": " & result(i) & Environment.NewLine
Next i

MessageBox.Show(distribution, "Distribution")
 
Interesting! I do still find the logic a curious one though - your brain definitely works in advanced ways.. Overall, I guess I never would have put that much engineering into what (IMHO) is a reasonably trivial problem. It's given me soemthing to think about with another problem that I faced a while ago, though.. so thanks! :D
 
Ok, ive got what i think its a simpler way (maybe not considered correct? Sombody please tell me if im wrong!)

VB.NET:
 Dim a As Integer = 0
        Dim my_rnd As Integer = 0


        Randomize()

        Do While a = 0
            my_rnd = 10 * Rnd()
            If my_rnd = 2 Then a = 0 Else a = 1
        Loop

Basically, loop until a number is generated that is not 2.
RND uses the result to generate the next number, so i don't think (by that logic) that it could keep generating 2 and looping forever?

Also whats worth noting, is that RND generates its first number off a seed, thats why you use randomize(), as it will create a different seed (not sure what it bases this on).
RND will return a value between 0 and 1 , which means we have to multiply that value by your maximum number. For example, 0.5 x 10 = 5 , 1 x 10 = 10 if that makes sense.

Im sure this is not the *best* method, but it should work?
 
LeonR, use the Random class instead of the legacy function Rnd() - I believe it's one of the 'things' kept for backward compability/upgrading of classic VB before .Net.
 
LeonR, use the Random class instead of the legacy function Rnd() - I believe it's one of the 'things' kept for backward compability/upgrading of classic VB before .Net.

Cool, learn somthing everyday!

I wasn't even aware there was a new class for this, DOH :eek:

VB6 is overpowering me from my youth!
 
Do enlighten me, how your code differs from mine? :)

owww, i was just wanting to type somthing :D

Seriously though, i should of read the other examples in a bit more depth rather than just jump in.

I have learnt that using RND is now considered obsolete though! :eek:
 
Back
Top