Click here to Skip to main content
15,896,538 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
As an assignment, I am trying to write the code for a lottery. I've run it a few times and for some reason, my loop only adds the same thing to my list box. Is it a problem with how I loop it? Or is it something else in my coding?
VB
Private Sub btnTry_Click(sender As Object, e As EventArgs) Handles btnTry.Click
       ' Declare variables.
       Dim intNum1 As Integer
       Dim intNum2 As Integer
       Dim intNum3 As Integer
       Dim intNum4 As Integer
       Dim intNum5 As Integer
       Dim strUserNum1 As String
       Dim strUserNum2 As String
       Dim strUserNum3 As String
       Dim strUserNum4 As String
       Dim strUserNum5 As String
       Dim blnCheck As Boolean = True
       Dim rand1 As New Random
       Dim intCompNum1 As Integer
       Dim intCompNum2 As Integer
       Dim intCompNum3 As Integer
       Dim intCompNum4 As Integer
       Dim intCompNum5 As Integer
       Dim strCompNum As String
       Dim intCount As Integer
       Dim strResult As String

       ' Convert the user's numbers and error check for repetition.
       Integer.TryParse(txtNum1.Text, intNum1)
       Integer.TryParse(txtNum2.Text, intNum2)
       Integer.TryParse(txtNum3.Text, intNum3)
       Integer.TryParse(txtNum4.Text, intNum4)
       Integer.TryParse(txtNum5.Text, intNum5)
       If intNum1 = intNum2 Or intNum2 = intNum3 Or intNum3 = intNum4 Or
           intNum4 = intNum5 Then
           lblStatus.Text = "Remember to not enter the same number twice."
           blnCheck = False
       End If

       ' Have the computer generate random numbers.
       intCompNum1 = rand1.Next(60) + 1
       intCompNum2 = rand1.Next(60) + 1
       intCompNum3 = rand1.Next(60) + 1
       intCompNum4 = rand1.Next(60) + 1
       intCompNum5 = rand1.Next(60) + 1

       ' Check to ensure random numbers are not the same.
       Do
           If Not intCompNum1 = intCompNum2 And intCompNum2 = intCompNum3 And
               intCompNum3 = intCompNum4 And intCompNum4 = intCompNum5 Then
               blnCheck = True
           Else
               blnCheck = False
               intCompNum1 = rand1.Next(60) + 1
               intCompNum2 = rand1.Next(60) + 1
               intCompNum3 = rand1.Next(60) + 1
               intCompNum4 = rand1.Next(60) + 1
               intCompNum5 = rand1.Next(60) + 1
           End If
       Loop Until blnCheck = True

       ' Start building the output string and check for matching numbers.
       For intCount = 1 To 10000
           strUserNum1 = CStr(intNum1)
           strUserNum2 = CStr(intNum2)
           strUserNum3 = CStr(intNum3)
           strUserNum4 = CStr(intNum4)
           strUserNum5 = CStr(intNum5)
           strCompNum = intCompNum1.ToString() & ", " & intCompNum2.ToString() &
               ", " & intCompNum3.ToString() & ", " & intCompNum4.ToString() &
               ", " & intCompNum5.ToString()
           strResult = "Attempt " & intCount.ToString() & ": You matched "
           If blnCheck = True Then
               If strCompNum.IndexOf(strUserNum1) <> -1 Then
                   strResult &= intNum1.ToString() & ", "
               End If

               If strCompNum.IndexOf(strUserNum2) <> -1 Then
                   strResult &= intNum2.ToString() & ", "
               End If

               If strCompNum.IndexOf(strUserNum3) <> -1 Then
                   strResult &= intNum3.ToString() & ", "
               End If

               If strCompNum.IndexOf(strUserNum4) <> -1 Then
                   strResult &= intNum4.ToString() & ", "
               End If

               If strCompNum.IndexOf(strUserNum5) <> -1 Then
                   strResult &= intNum5.ToString
               End If

               ' Add the input to the lstOutput box.
               lstOutput.Items.Add(strResult)
           End If
       Next
   End Sub
Posted
Updated 22-Mar-15 6:17am
v2

1 solution

First off, start by moving the Random class instance to outside the method - make it a Private class level variable instead. That way, you aren't creating it each time he presses the button, so you should get a better random sequence.
Then, create a List of integers and fill it with the ball numbers. That's easy:
VB
Dim balls As List(Of Integer) = Enumerable.Range(1, 49).ToList()

Then generate six random numbers from the list:
VB
Dim balls As List(Of Integer) = Enumerable.Range(1, 49).ToList()
Dim winner As New List(Of Integer)()
For i As Integer = 0 To 5
    Dim winBall As Integer = Random.[Next](0, balls.Count)
    winner.Add(balls(winBall))
    balls.RemoveAt(winBall)
Next
Becaue you use a list of all balls, and remove each one when it's selected, you are guaranteed not to get any repetitions.

Then all you have to do is sort the user input numbers, sort the winning ball numbers, and you can compare them very simply!

Make sense?
 
Share this answer
 
Comments
Devonte Dereef 22-Mar-15 12:01pm    
Oh okay. So the problem here was where I had placed the random variables? Now that I have edited it the result is different now. Thank you.
OriginalGriff 22-Mar-15 12:32pm    
Mostly, yes - When you create a Random instance it is initialized with a seed value from the system clock, so if you want "real" pseudo random sequence, you need to create a single instance, and use it repeatedly.

But it's worth using a List anyway - because it means that the time taken to get "n" random balls is the always the same. If you generate sets of random numbers until you get one with no duplicates, you can get into the position when it takes hours to get a single sequence. It's unlikely when picking 6 out of 49, but the larger the ball set you pick the more frequently "collisions" happen.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900