Hello everyone,

I have a little problem that drove me crazy I can't figure it out, I spent hours searching the Web but in vain. I hope that you can help with it. The program must generate 6 unique random numbers but when I click display numbers sometimes it gives me 6 unique numbers and sometimes I get duplicate numbers. I will add the code I have so far and I hope that you can help me out.

Public Class frmMain

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click

        ' close the application
        Me.Close()
    End Sub

    Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click



        Dim intRandom1 As Integer
        Dim intRandom2 As Integer
        Dim intRandom3 As Integer
        Dim intRandom4 As Integer
        Dim intRandom5 As Integer
        Dim intRandom6 As Integer
        Dim randomGenerator As New Random

        ' generate random integers 

        intRandom1 = randomGenerator.Next(1, 55)
        intRandom2 = randomGenerator.Next(1, 55)
        intRandom3 = randomGenerator.Next(1, 55)
        intRandom4 = randomGenerator.Next(1, 55)
        intRandom5 = randomGenerator.Next(1, 55)
        intRandom6 = randomGenerator.Next(1, 55)


        ' display integers
        lblNumbers.Text = Convert.ToString(intRandom1) & "   " &                                              Convert.ToString(intRandom2) & "   " & Convert.ToString(intRandom3) & "   " & Convert.ToString(intRandom4) & "   " & Convert.ToString(intRandom5) & "   " & Convert.ToString(intRandom6)

        
    End Sub
End Class

Thank you so much

' Declare at form level
   Dim objRandom As New System.Random( _
   CType(System.DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer))

   Public Function GetRandomNumber( _
    Optional ByVal Low As Integer = 1, _
    Optional ByVal High As Integer = 100) As Integer
        ' Returns a random number,
        ' between the optional Low and High parameters
        Return objRandom.Next(Low, High + 1)
    End Function

' Using the above function to generate Random numbers.  
  Dim intDiceRoll As Integer
        intDiceRoll = GetRandomNumber(1, 1000000)
        MessageBox.Show("You rolled a " & intDiceRoll.ToString)

If you have already covered arrays then I suggest you use an array to hold the random numbers. You start with no random numbers in the array. Your outer loop will terminate when you have generated the 6 required unique numbers. Inside the loop you can generate a random number and add it to the array only if it isn't already present in the array.

Does that make it a little clearer?

Rev Jim is right, check your algorithm, when you're simply assigning each of them to a random number there's no guarantee that they will all be different.

Have an empty array
while the length of the array is less than six
generate a random number
if that random number isn't in the array add it
else pass and get another random number and try again
when the array is at length six~End

I would differ with that only slightly. Your pseudo-code has a test at the top AND bottom of the loop. I would rewrite it as

start with an empty array
while the length of the array is less than 6
    generate a random number
    if that number is not in the array
        add it to the array
    end if
end while

haha, I'm just starting to learn VB I was going on pythonic pseudo-code, where we don't have to end the while.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.