Question How do I generate an array of 5 random numbers (with no repeates)

Mixelplik

Member
Joined
Dec 12, 2009
Messages
5
Programming Experience
Beginner
I'm trying to make a Bulls and Cows type game, I was able to do it fairly easily with javaScript, but VB is killing me. I can get the array 5 slots long with random numbers, unfortunately they're all repeats. when I try getting no repeats I run into infinite loops, exponentially increasing array lengths, and just plain old arrays with 5 numbers randomly generated, but with repeats.

I'm trying to do this with loops, in JS I did it using regular expressions, but I have no clue how to use them in VB.

Here's an example of my code.

Please note, I'm a beginner - so please be patient. Thanks for anyhelp.

VB.NET:
Public Class Form1
    Dim intRand(0) As Integer

    Dim RandomClass As New Random


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ReDim intRand(0)
        intRand(0) = GenNum()
        Dim intTempRand As Integer = 0
        Dim intCounter As Integer = 0
        '     lst5Rand.Items.Clear()

        'Loop Outter
        For i As Integer = 0 To 4
            'Loop Inner
            intTempRand = GenNum()
            For y As Integer = 0 To intRand.Length
                If intTempRand = intRand(y) Then
                    lbl1.Text = "Duplicate"
                    Do While intTempRand = intRand(y)
                        intTempRand = GenNum()
                    Loop
                ElseIf intTempRand <> intRand(y) Then
                    lbl1.Text = Convert.ToString(intRand.Length)
                    ReDim Preserve intRand(intRand.Length + 1)
                    intRand(i + 1) = intTempRand
                End If
                ReDim Preserve intRand(intRand.Length + 1)
            Next
            lst5Rand.Items.Add(intRand(i))

        Next

    End Sub

    Private Function GenNum() As Integer
        Return RandomClass.Next(0, 4)
    End Function


End Class
 
This one:

Enumerable.Range(0, 10).OrderBy(Function() rnd.Next)
There are three function calls in that line (Range, OrderBy, Next) ;) Enumerable class I have already explained to you. The inline "Function" statement defines a lambda expression, also new in VB 2008 and is also supportive for LINQ, see Lambda Expressions.
 
{Be hAppy}

Can you please tell me what is the problem with following code that provide full surity that it will pick 5 unique (No repeatation is possible) random numbers.


Private Sub Show5UniqueRandoms()
Dim n As Integer = 0
Dim C As New Collection
Randomize(Timer)
Do
Try
Dim Data = Rnd()
C.Add(Data, "K" & Data)
n += 1
Catch ex As Exception
End Try
Loop Until n = 5
Dim m As Object
For Each m In C
MsgBox(m)
Next
End Sub

Show5UniqueRandoms
Explaination:

The Collection can not have two items with same key. If same Random number is generated again it will raise the error so n value will not change and item is not added to collection.

2nd Randomize Timer is important other wise always you will receive the same result.
 
There is a pre defined class available for the generation of random numbers.

SYNTAX:

dim obj as new Random

obj.next(minvalue,maxvalue)
 
{Be hAppy}

Agree ...

Where its written that the class can not generate duplicate entries?
At the end again you need to take a collection or any other way...

Also I am using RND() function - not producing my own random numbers ..

OK Let me use the function you are telling me:

Private Sub Show5UniqueRandoms()
dim MinValue as Integer = 1000
dim MaxValue as Integer = 9999
dim obj as new Random
Dim n As Integer = 0
Dim C As New Collection
Randomize(Timer)
Do
Try
Dim Data = obj.next(minvalue,maxvalue)
C.Add(Data, "K" & Data)
n += 1
Catch ex As Exception
End Try
Loop Until n = 5
Dim m As Object
For Each m In C
MsgBox(m)
Next
End Sub


By the way:

MinValue + RND*(MaxValue-MinValue)

will also give you same result ....
 
Back
Top