Results 1 to 30 of 30

Thread: [RESOLVED] [2005] Working with arrays; Lottery number generator for class

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Posts
    31

    Resolved [RESOLVED] [2005] Working with arrays; Lottery number generator for class

    Ok, I have just finished my "Introduction to Visual Basic" course, and overall did pretty good (A's and B's on assignments). However, one of the last assignments threw a wrench into the mix and I wasn't able to finish one of three projects.

    Text book is Programming with Microsoft Visual Basic 2005: Third Edition (if that matters) with my book got a CD with Visual Basic 2005 Expess Edition (not Visual Studio 2005 Express, CD lacked any other languages). Chapter 9: Arrays, Lesson A, Excercise number 14:

    "In this exercise you create an application that generates and displays six unique random numbers for a lottery game. Each lottery number can range from 1 through 54 only.
    A) start VB, open partial program, etc.

    B)Open the Display Numbers button's Click event procedure. Code the procedure so that it displays six unique numbers int eh interface. (Hint: Store the numbers in a one-dimensional array.)

    C) Save, start application, click the Display numbers button several times. Each time you click the button, six unique random numbers between 1 and 54 (inclusive) should appear in the interface.

    D) exit program, close code editor and solution"

    So, here is what I have:

    Code:
    Private Sub xDisplayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xDisplayButton.Click
    
            ' declare array and other variables
    
            Dim lottoNumbers() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, _
                                            17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, _
                                            31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, _
                                            45, 46, 47, 48, 49, 50, 51, 52, 53, 54}
            Dim lottoNumber As Integer
            Dim number As Integer
            'Dim rad1 As Integer
           ' Dim rad2 As Integer
            'Dim rad3 As Integer
           ' Dim rad4 As Integer
            'Dim rad5 As Integer
            'Dim rad6 As Integer
    
    
            ' randomly select 6 unique random numbers from array
    
            'For Each number In lottoNumbers
            *****************
            *****************
            'Next number
            'lottoNumber = number
    
            'lottoNumber = rad1 & "-" & rad2 & "-" & rad3 & "-" & rad4 & "-" & rad5        & "-" & rad6
    
    
            ' display numbers in lottery label box
    
            Me.xLotteryLabel.Text = CStr(lottoNumber)
    
    
        End Sub
    So, above I declared my array containing 1 - 54, I declared a variable named lottoNumber and one called number to get numbers from array and then display in label. Notice the ' in front of some code, these are the areas that I am not sure of (along with lines that contain *'s), and therefore marked them to the side. I tried using random numbers (by declaring ' Dim randomgenerator As New random' then using in code. I got it to work, but I had 0's in my output, and therefore, it wasn't randomizing my array. If I try 'randomize (lottoNumbers)' it gives me an error saying "value of type '1-dimensional array of Integer' cannot be converted to 'Double' "

    Anyways, I've tried my best to figure this one out (about 12 hours now) and knowing that I am a beginner (by a long shot) I just couldn't wrap my head around what I needed to do to finish this program. This is all the code I have to mess with (I've wrote all of it, none of this was done when I started) so it should have been simple (not 15 pages long or anything), not to mention I was in Introduction class (not even in Programming in Visual Basic yet).

    As far as class goes, I'll pass no problems with an easy B without this program being completed, however, for my own sanity, I want to understand so that I can finish this stupid, simple, and annoying program.

    Anyone know what I need to do?

    Thank you, and sorry if long post, didn't know how much background was needed.

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Working with arrays; Lottery number generator for class

    You would use the Random class to generate a random number. Altough I dont see the reason why you would want to store the numbers 1 through 54 in an array, are you sure they dont mean that the 6 generated numbers should be placed in an array?
    Anyway, since its best for your learning, I wont give you any code, but a hint:
    Generate a random number, does it exist in the array of already generated numers? If no, place it in the array, generate next number. If yes, generate another new number and try again.

    Altough there are "smarter" ways of doing this, that'll guarantee you to never generate the same number twice, but I think it'll be "overkill" in this case.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: [2005] Working with arrays; Lottery number generator for class

    Not overkill, required. The assignment specifically states that the numbers must be unique.

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Working with arrays; Lottery number generator for class

    Quote Originally Posted by MaximilianMayrhofer
    Not overkill, required. The assignment specifically states that the numbers must be unique.
    I know, if you read my 'hint' to tonyp56, you'll see that it will generate unique numbers. Im just saying that there is a more efficient way to do this, that will generate unique numbers directly (ie, you dont need to check if its unique after generating it).
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: [2005] Working with arrays; Lottery number generator for class

    Is there? Post it, because the only method so far that i've managed to come up with to ensure uniqueness is yours. Unless you want to manipulate the seed directly.

  6. #6
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Working with arrays; Lottery number generator for class

    Well, its nothing more fancy than creating a List(Of Integer) to hold the values 1 through 54, then iterate through a loop 6 times that will pick a random index from 0 to the lists upper bound. Add the number at the generated to the array where you'll store your numbers, then remove it from the list.

    Think of it like this, if you want to pick out 6 different shirts from your closet without looking, you wouldnt take out the shirts one at a time, noting which shirt it is on a piece of paper, then put the shirt back in the closet again. No, you'd put that shirt aside to assure yourself that you wont pick it again.

    Altough since he's only looking to generate 6 random numbers out of 54 available, chances are not that great that he'll generate alot of identical numbers, thats why I said this method was a bit overkill.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Posts
    31

    Re: [2005] Working with arrays; Lottery number generator for class

    Quote Originally Posted by Atheist
    You would use the Random class to generate a random number. Altough I dont see the reason why you would want to store the numbers 1 through 54 in an array, are you sure they dont mean that the 6 generated numbers should be placed in an array?
    I don't know, I copied question from text book in above text, what you see is what I see (nothing else tells me if I should make array for 54 numbers or 6 random and unique numbers).

    Quote Originally Posted by Atheist
    Anyway, since its best for your learning, I wont give you any code, but a hint:
    Generate a random number, does it exist in the array of already generated numers? If no, place it in the array, generate next number. If yes, generate another new number and try again.

    Altough there are "smarter" ways of doing this, that'll guarantee you to never generate the same number twice, but I think it'll be "overkill" in this case.
    Thanks for the hint, I will re-write and see what I can figure out hopefully being able to get working. I've already had it working, but it required me to use my six "rad" variables. Writing
    Code:
    number = randomgenerator.next (1,  54) _
    rad1 = number _
    number = randomgenerator.next (1, 54) _
    rad2 = number
    etc...
    lottoNumber = rad1 & "-" & rad2 & "-" & rad3 & "-" & rad4 & "-" & rad5        & "-" & rad6
    (please note, I am still learning code, and therefore some of my syntax is likely not correct above, not to mention I am not in front of text book or Visual basic right now, and going from memory of what I did among the tons of stuff I've done to try to get program working)

    This of course, gave me 6 numbers, though I did/could have had repeated numbers. I could clean that up with just adding a If/Else/EndIf statement correct? and instead of outputting to rad1, rad2, rad3, ..., output to array, and then just output from array to my label.

    Yes I am learning, right now, I want to know I finished this class, including all assignments. Though I want to go beyond my college work and write in Visual Basic on my own. This of course, requires that I come up with good ideas for programs to write (so I'll be around here in the coming months a lot-- Not to mention when I start my programming in Java, C/C++, and Visual Basic later. Programming in VB will be the one I do next just to keep things along the same lines).
    Last edited by tonyp56; Mar 2nd, 2008 at 12:51 PM.

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [2005] Working with arrays; Lottery number generator for class

    It seems to me that there is an easier way than what Atheist had mentioned, because you need a very small subset of the numbers 1 to 54.

    I would create an array (actually, I would use a List of Integer, but the OP mentioned arrays, so let's use arrays) of six integers. Then I would perform a pair of nested loops. The outer loop is a Do While loop that gets a random number between 1 and 54, then iterates through the integer array looking to see if the number is in the array, if it isn't, then add it to the array, if it is, then just go on. The outer loop runs until six numbers have been added to the array.

    This would be a really bad way to do things if the number sought approaches the search space, but with a search space of 54, and a number sought of only 6, the odds of getting a repeat number, even on the 6th number, is only a bit less than 1 in 8. Since the loop will be fast, and the logic simple, I think this is reasonable odds.

    For the first number, the chances of getting a duplicate are 0
    For the second number, the chances of getting a duplicate are 1:54
    etc.
    My usual boring signature: Nothing

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Posts
    31

    Re: [2005] Working with arrays; Lottery number generator for class

    Quote Originally Posted by Shaggy Hiker
    I would create an array (actually, I would use a List of Integer, but the OP mentioned arrays, so let's use arrays) of six integers.
    The only reason I mentioned arrays is because it was the assignment I had required the use of an array. I have not learned about List of Integer, so that wouldn't have helped anyways.
    On creating an array do you mean create an array by declaring
    Code:
    Dim lottoNumbers (6) As Integer
    or would I have to populate array?
    Code:
    Dim lottoNumber (6) As Integer = {1, 3, 6, 35, 53, 2}
    Sorry if this is stupid question, I am just wanting to verify what you are meaning, thanks. (believe first one would be the code I would want to write, because if I write the second code wouldn't program never display any numbers except those in the array?)

    Quote Originally Posted by Shaggy Hiker
    Then I would perform a pair of nested loops. The outer loop is a Do While loop that gets a random number between 1 and 54, then iterates through the integer array looking to see if the number is in the array, if it isn't, then add it to the array, if it is, then just go on. The outer loop runs until six numbers have been added to the array.
    Not sure of the syntax, but I did something like this
    Code:
    number = (rnd()1 * 54) + 1)
    which gave me a random number between 1 and 54, would I want to set up a Do While Loop that would have that (or something similar) in it to get random numbers until there is 6 total random numbers, then do a nested Do While Loop that compares random number to number(s) already in array? Do I have the idea, or am I missing it completely?

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [2005] Working with arrays; Lottery number generator for class

    The array needs to be:

    Dim lottoNumbers(5) as Integer

    since the array is 0 to 5 rather than 1 to 6. You also don't want to populate the array first, because you will be populating it in the loop.

    Why did you switch from the Random class that you showed in post #7 back to Rnd, which is a deprecated VB6 function that is clearly inferior to Random.

    Otherwise, you're on the right track. Get one random number, see whether it is in the array, and if it is not, add it. If it is in the array, then get another random number, and so forth.
    My usual boring signature: Nothing

  11. #11
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Working with arrays; Lottery number generator for class

    Quote Originally Posted by Shaggy Hiker
    It seems to me that there is an easier way than what Atheist had mentioned, because you need a very small subset of the numbers 1 to 54.

    I would create an array (actually, I would use a List of Integer, but the OP mentioned arrays, so let's use arrays) of six integers. Then I would perform a pair of nested loops. The outer loop is a Do While loop that gets a random number between 1 and 54, then iterates through the integer array looking to see if the number is in the array, if it isn't, then add it to the array, if it is, then just go on. The outer loop runs until six numbers have been added to the array.

    This would be a really bad way to do things if the number sought approaches the search space, but with a search space of 54, and a number sought of only 6, the odds of getting a repeat number, even on the 6th number, is only a bit less than 1 in 8. Since the loop will be fast, and the logic simple, I think this is reasonable odds.

    For the first number, the chances of getting a duplicate are 0
    For the second number, the chances of getting a duplicate are 1:54
    etc.
    Yeah that's what i ment in my "hint" in post #2 My explanation was probably a bit vague
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [2005] Working with arrays; Lottery number generator for class

    I thought you were suggesting that he go after a more robust means of choosing numbers. This particular technique will become REALLY inefficient as the number chosen begins to reach even half the search space.
    My usual boring signature: Nothing

  13. #13
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Working with arrays; Lottery number generator for class

    Quote Originally Posted by Shaggy Hiker
    I thought you were suggesting that he go after a more robust means of choosing numbers. This particular technique will become REALLY inefficient as the number chosen begins to reach even half the search space.
    Yeah, I figured the "robust" approach would be a bit overkill for this small purpose, and especially when he only wants 6 numbers out of 51 possible. As you pointed out, the odds for getting repeated numbers are fairly small.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Posts
    31

    Re: [2005] Working with arrays; Lottery number generator for class

    Quote Originally Posted by Shaggy Hiker
    The array needs to be:

    Dim lottoNumbers(5) as Integer

    since the array is 0 to 5 rather than 1 to 6. You also don't want to populate the array first, because you will be populating it in the loop.

    Why did you switch from the Random class that you showed in post #7 back to Rnd, which is a deprecated VB6 function that is clearly inferior to Random.

    Otherwise, you're on the right track. Get one random number, see whether it is in the array, and if it is not, add it. If it is in the array, then get another random number, and so forth.
    Yes, the first time I went through it I used the Random class, then in trying to get it to work and searching throughout the internet, found the Rnd function (other places showed the syntax for that, and not so much for the random class). Which lead me to that--thought perhaps it was a better way of doing it???--however, I will make sure to use the random class when I write my code. Thanks

    Will be back on here in a bit hopefully with working code (fingers are crossed). Haven't had time to work on this yet today, so will in a bit...

  15. #15
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [2005] Working with arrays; Lottery number generator for class

    Rnd was the VB6 version of Random. There is much more out there about using Rnd, because it was much more difficult to use well, and harder to understand. That crazy calculation to get a range is nowhere near as simple as Random.GetNext, which just "gets a random number between the first argument and the second argument." That's soooo much simpler.
    My usual boring signature: Nothing

  16. #16
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [2005] Working with arrays; Lottery number generator for class

    Code:
            Dim rndNum As New Random, whSelected As New BitArray(55)
            Dim pick As Int32, ctPicks As Int32 = 0, theNumbers As String
            theNumbers = ""
            Do While ctPicks < 6 'pick six unique numbers
                pick = rndNum.Next(1, 55) 'generate a random number
                If Not whSelected(pick) Then 'have we already picked it
                    whSelected(pick) = True 'no, mark it as picked
                    ctPicks += 1 'increment count of picks
                    theNumbers &= pick.ToString & "  "
                End If
            Loop
            Debug.WriteLine(theNumbers)
            whSelected.SetAll(False) 'get ready for next run
    Last edited by dbasnett; Mar 3rd, 2008 at 06:40 PM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Posts
    31

    Re: [2005] Working with arrays; Lottery number generator for class

    Ok, I am working with this, and I'm having trouble (please bare with me)...
    My label now displays "System.Int32[]" when I click my display button.

    Here is my updated code:

    Code:
    Private Sub xDisplayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xDisplayButton.Click
    
            ' declare array and other variables
    
            Dim lottoNumbers(5) As Integer
            Dim counter As New Integer
            Dim number As Integer
            Dim randomGenerator As New Random
    
    
            ' randomly select 6 unique random numbers from array
    
            
            Do While counter < 6
                For Each number In lottoNumbers
                    number = randomGenerator.Next(1, 55)
                    If lottoNumbers(0).ToString.Contains(CStr(number)) Then
                        number = randomGenerator.Next(1, 55)
                    Else
                        counter = counter + 1
                        lottoNumbers(0) = number
                    End If
    
                Next
            Loop
    
    
    
    
            ' display numbers in lottery label box
    
            Me.xLotteryLabel.Text = lottoNumbers.ToString
    
    
    
    
    
    
        End Sub
    Still trying to think it out, and I am trying my very best, but having some trouble. Anyone?

    BTW thank you to everyone for all of your help getting me this far...
    Last edited by tonyp56; Mar 3rd, 2008 at 12:57 AM.

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Posts
    31

    Re: [2005] Working with arrays; Lottery number generator for class

    still working:

    vb Code:
    1. Private Sub xDisplayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xDisplayButton.Click
    2.  
    3.         ' declare array and other variables
    4.  
    5.         Dim lottoNumbers(5) As Integer
    6.         Dim randomGenerator As New Random
    7.         Dim counter As New Integer
    8.         Dim number As Integer
    9.        
    10.  
    11.         ' randomly select 6 unique random numbers from array
    12.  
    13.         Do Until counter = (6)
    14.             For Each number In lottoNumbers
    15.                 number = randomGenerator.Next(1, 55)
    16.                 If lottoNumbers.ToString.Contains(CStr(number)) Then
    17.                     number = randomGenerator.Next(1, 55)
    18.                 Else
    19.                     counter = counter + 1
    20.                     lottoNumbers(0) = number
    21.                 End If
    22.             Next number
    23.         Loop
    24.  
    25.         ' display numbers in lottery label box
    26.         Me.xLotteryLabel.Text = lottoNumbers.ToString
    27.  
    28.  
    29.     End Sub
    Not much different, but trying my best to change things around hoping something will click...

  19. #19
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [2005] Working with arrays; Lottery number generator for class

    Did you look at my example?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  20. #20
    Frenzied Member
    Join Date
    May 2006
    Location
    Toronto, ON
    Posts
    1,093

    Re: [2005] Working with arrays; Lottery number generator for class

    Quote Originally Posted by tonyp56
    Ok, I am working with this, and I'm having trouble (please bare with me)...
    My label now displays "System.Int32[]" when I click my display button.
    That's because that's what you're telling it to display. It's saying that lottonumbers is an array of integers, which is what the ToString() method returns for an array.

    What you need to do is create a string variable and loop through the array, adding each value to that variable and then display the variable.

  21. #21
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Working with arrays; Lottery number generator for class

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ' declare array and other variables
            Dim lottoNumbers(5) As Integer
            Dim counter As Integer = 0
            Dim number As Integer
            Dim randomGenerator As New Random(Now.Millisecond)
    
            ' randomly select 6 unique random numbers from array
            Do While counter < 6
                number = randomGenerator.Next(1, 55)
                If Array.IndexOf(lottoNumbers, number) = -1 Then
                    lottoNumbers(counter) = number
                    counter += 1
                End If
            Loop
    
            ' display lottery numbers in label box
            Me.Label1.Text = String.Empty
            Array.Sort(lottoNumbers)
            For Each num As Integer In lottoNumbers
                Me.Label1.Text &= CStr(num) & "  "
            Next num
        End Sub

  22. #22

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Posts
    31

    Re: [2005] Working with arrays; Lottery number generator for class

    Quote Originally Posted by dbasnett
    Did you look at my example?
    Yes, I did, and tried to use what you had, however, wasn't able to get it to work (it gave errors on a few areas and kept it from working) I have to have option Explicit and strict on while writting programs.

    Thank you though it gave me some ideas to try.

  23. #23

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Posts
    31

    Re: [2005] Working with arrays; Lottery number generator for class

    Quote Originally Posted by Tom Sawyer
    That's because that's what you're telling it to display. It's saying that lottonumbers is an array of integers, which is what the ToString() method returns for an array.

    What you need to do is create a string variable and loop through the array, adding each value to that variable and then display the variable.
    Should have known, thank you for pointing that out, again, just learning VB, and not really sure of how to use different things.

    Thanks

  24. #24
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [2005] Working with arrays; Lottery number generator for class

    Quote Originally Posted by tonyp56
    I have to have option Explicit and strict on while writting programs.
    That's a very good thing!
    My usual boring signature: Nothing

  25. #25

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Posts
    31

    Thumbs up Re: [2005] Working with arrays; Lottery number generator for class

    Quote Originally Posted by VBDT
    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ' declare array and other variables
            Dim lottoNumbers(5) As Integer
            Dim counter As Integer = 0
            Dim number As Integer
            Dim randomGenerator As New Random(Now.Millisecond)
    
            ' randomly select 6 unique random numbers from array
            Do While counter < 6
                number = randomGenerator.Next(1, 55)
                If Array.IndexOf(lottoNumbers, number) = -1 Then
                    lottoNumbers(counter) = number
                    counter += 1
                End If
            Loop
    
            ' display lottery numbers in label box
            Me.Label1.Text = String.Empty
            Array.Sort(lottoNumbers)
            For Each num As Integer In lottoNumbers
                Me.Label1.Text &= CStr(num) & "  "
            Next num
        End Sub
    Thank you very much for the help. Now that I see code (have used everything that you have in code before) realize that I was missing the target. Perhaps not by far, but far enough.

    Thank you very much for the help...

  26. #26

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Posts
    31

    Re: [2005] Working with arrays; Lottery number generator for class

    To everyone, thank you very much for the help. Program is working now, like it is supposed to and with no issues.

    Thanks everyone

  27. #27
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [2005] Working with arrays; Lottery number generator for class

    Quote Originally Posted by tonyp56
    Yes, I did, and tried to use what you had, however, wasn't able to get it to work (it gave errors on a few areas and kept it from working) I have to have option Explicit and strict on while writting programs.

    Thank you though it gave me some ideas to try.
    I fixed it, if you want to go back and look.

    Agree about strict on.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  28. #28

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Posts
    31

    Re: [2005] Working with arrays; Lottery number generator for class

    Quote Originally Posted by dbasnett
    I fixed it, if you want to go back and look.

    Agree about strict on.
    Thank you, I will save code so I can see a different way of doing this in the future.

  29. #29

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Posts
    31

    Re: [2005] Working with arrays; Lottery number generator for class

    Quote Originally Posted by Shaggy Hiker
    That's a very good thing!

    Just as well to get into the habbit now (when I am learning).

    One question if you don't mind, do I have to declare it within code? Or simply having these two options turned on in VB general properties good enough? Textbook more or less said that I should type it into general declarations section for every program I write, even though the text book said you can turn on/off through properties pages. Just wondering...

  30. #30
    New Member
    Join Date
    Jan 2020
    Posts
    1

    Re: [RESOLVED] [2005] Working with arrays; Lottery number generator for class

    hi, as a beginner in VB.NET, i'm offering my code for original problem, even 12 years later, though

    1 Option Explicit On
    2 Option Strict On
    3 Option Infer Off
    4 ' you code an app that generates & displays 6 UNIQUE RANDOM numbers for a lottery game
    5 ' each lottery number can range: 1-54 only
    6 ' btnDisplay_Click procedure should display in GUI
    7
    8 ' lblLottery
    9 'CH7_A3 info about Random, 31_15 Exercise
    10
    11 Public Class frmMain
    12
    13 'Private intAccumulator(5) As Integer
    14
    15 Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
    16 ' Generates and displays six unique random numbers from 1 through 54.
    17
    18 lblLottery.Text = Nothing
    19
    20 Dim intAccumulator(5) As Integer
    21 Dim intRandGen As New Random
    22 Dim intRandomNumber As Integer
    23
    24 For intCounter As Integer = 0 To 5
    25 intRandomNumber = intRandGen.Next(1, 55)
    26 For intCheck As Integer = 0 To intCounter
    27 If intRandomNumber <> intAccumulator(intCheck) Then
    28 intAccumulator(intCounter) = intRandomNumber
    29 Else
    30 intRandomNumber = intRandGen.Next(1, 55)
    31 End If
    32 Next intCheck
    33 lblLottery.Text &= intAccumulator(intCounter).ToString & " "
    34 Next intCounter
    35
    36 End Sub
    37
    38 Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
    39 Me.Close()
    40 End Sub
    41
    42 End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width