It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming Visual Basic Forum

Little help with homework please

Moderator: dafunkymunky

Little help with homework please

Postby pfm200586 on Fri Oct 07, 2011 4:04 am

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.


Code: Select all
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
pfm200586
 
Posts: 3
Joined: Thu Sep 08, 2011 1:17 pm

Re: Little help with homework please

Postby ciiure on Fri Nov 18, 2011 8:31 pm

Its because random function picks random numbers and sometimes it can give you same number.
you could use some sort of check function such as if number1 is same as any other number then repick number.
ciiure
 
Posts: 5
Joined: Fri Nov 18, 2011 7:45 pm


Who is online

Users browsing this forum: No registered users and 1 guest