Results 1 to 28 of 28

Thread: [RESOLVED] VB6 Lottery

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2006
    Posts
    5

    Resolved [RESOLVED] VB6 Lottery

    Hello. I'm pretty much new to VB and I'm currently on an IT course at college. In one of our units we are learning VB and I've been trying to do this task which has started to prove quite difficult. I've tried to seek advice and help from tutors and fellow students, but the students are quite disruptive in class except a few and our tutors just got changed.

    I basically have to create a lottery program, that will have a button that creates 6 random numbers in text box lbls and a button for the bonus ball, as well as a sort button to sort the 6 numbers in ascending order. I've done all of this except, my 6 numbers sometimes turn out the same as well as the bonus ball and my reset button resets all 6 to 0.

    Any help would be appreciated, as I'm really lost right now and kind of in the deep end with all this being so new to me. Heres what I have so far. I've tried variations of generating the 6 numbers but this is what I originally had.
    -------------------------------------------------------------------------

    Dim Values(1 To 6) As Integer

    Private Sub cmdReset_Click() 'Resets lottery numbers'
    lbl1.Caption = "0"
    lbl2.Caption = "0"
    lbl3.Caption = "0"
    lbl4.Caption = "0"
    lbl5.Caption = "0"
    lbl6.Caption = "0"
    cmdReset.Visible = False 'Reset button is invisible'
    End Sub

    Private Sub cmdBonus_Click()
    Randomize
    Dim counter As Integer
    lblBonus.Caption = Int(Rnd * 16 + 1)
    For counter = 1 To 6
    If Values(counter) = lbl1.Caption Or lbl2.Caption Or lbl3.Caption Or lbl4.Caption Or lbl5.Caption Or lbl6.Caption Then
    lblBonus.Caption = Int(Rnd * 16 + 1)
    End If

    Next

    End Sub

    Private Sub cmdBalls_Click()
    Randomize
    Dim counter As Integer
    cmdReset.Visible = True
    lbl1.Caption = Int(Rnd * 16 + 1)
    lbl1.Caption = Values(1)
    lbl2.Caption = Int(Rnd * 16 + 1)
    For counter = 1 To 6
    If Values(counter) = lbl2.Caption Then
    lbl2.Caption = Int(Rnd * 16 + 1)
    counter = 1
    End If

    Next

    lbl3.Caption = Int(Rnd * 16 + 1)
    For counter = 1 To 6
    If Values(counter) = lbl3.Caption Then
    lbl3.Caption = Int(Rnd * 16 + 1)
    counter = 1
    End If

    Next

    lbl4.Caption = Int(Rnd * 16 + 1)
    For counter = 1 To 6
    If Values(counter) = lbl4.Caption Then
    lbl4.Caption = Int(Rnd * 16 + 1)
    counter = 1
    End If

    Next

    lbl5.Caption = Int(Rnd * 16 + 1)
    For counter = 1 To 6
    If Values(counter) = lbl5.Caption Then
    lbl5.Caption = Int(Rnd * 16 + 1)
    counter = 1
    End If

    Next

    lbl6.Caption = Int(Rnd * 16 + 1)
    For counter = 1 To 6
    If Values(counter) = lbl6.Caption Then
    lbl6.Caption = Int(Rnd * 16 + 1)
    counter = 1
    End If

    Next

    End Sub


    Private Sub cmdSort_Click()
    Dim counter As Integer
    Dim sortcount As Integer
    Dim sort As Integer

    counter = 1
    sortcount = 1

    Do Until counter = 100
    sortcount = 1
    Do Until sortcount = 6
    If Values(sortcount) > Values(sortcount + 1) Then
    sort = Values(sortcount)
    Values(sortcount) = Values(sortcount + 1)
    Values(sortcount + 1) = sort
    End If
    sortcount = sortcount + 1
    Loop
    counter = counter + 1
    Loop
    lbl1.Caption = Values(1)
    lbl2.Caption = Values(2)
    lbl3.Caption = Values(3)
    lbl4.Caption = Values(4)
    lbl5.Caption = Values(5)
    lbl6.Caption = Values(6)

    End Sub

  2. #2
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: VB6 Lottery

    First, you're telling the Reset command to make all the labels 0.

    Second, you'd be better off using a control array of labels. It would make analyzing them a hell of a lot easier.

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2006
    Posts
    5

    Re: VB6 Lottery

    Oh wait.... sorry.... I meant sort button.... my sort button resets all numbers to 0 lol.... really tired ><

    An array huh? Well I'll give that a shot, I'm going to be ringing my friend in a min.... hes working on it too. Hes had more experience but decided to do it that way and helped me a bit with it. I'll probably mention an array to him, as we are more less at the same point.

    Thanks for that..... hopefully I can get somewhere now.
    Last edited by Phurbu; Dec 3rd, 2006 at 03:16 PM.

  4. #4
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: VB6 Lottery

    Welcome

    In addition, i don't believe that the "If Values(counter) = ...Or...Or...Or" statement you have does what you think it does. You need to compare each one:
    VB Code:
    1. '...
    2. If Values(counter) = lbl1.Caption Or Values(counter) = lbl2.Caption Or Values(counter) = lbl3.caption '...
    3. '...

  5. #5
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: VB6 Lottery

    I remember making that mistake when i first started coding... gotta remember that conditions don't carry on in the statement. you have to check everything manually.

  6. #6
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: VB6 Lottery

    Quote Originally Posted by timeshifter
    I remember making that mistake when i first started coding... gotta remember that conditions don't carry on in the statement. you have to check everything manually.
    Exactly. If you do it the way he did it (...value Or value...) Or returns a number, which would lead to type mismatch error.

  7. #7

    Thread Starter
    New Member
    Join Date
    Dec 2006
    Posts
    5

    Re: VB6 Lottery

    Thanks, I really appreciate the help. I'll let you know how things go, got quite a few assignments going on here, so this is greatly appreciated.

  8. #8
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: VB6 Lottery

    I think this is the gist of what you're after... uses a control array of labels and a simple binary sort to get them in order. Works like a charm. I hope it's what you're looking for. Easy code, too.
    Last edited by timeshifter; Mar 23rd, 2007 at 09:33 AM.

  9. #9

    Thread Starter
    New Member
    Join Date
    Dec 2006
    Posts
    5

    Re: VB6 Lottery

    Wow thankyou. This has really helped me. Thanks so much, exactly what I was looking for.

    I think I can finally start getting my heard around this VB lol.

  10. #10
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: VB6 Lottery

    Glad to be of service.

  11. #11
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 2004
    Location
    Staffordshire, England
    Posts
    2,236

    Re: VB6 Lottery

    @timeshifter - you need to check that the numbers haven't already been used the array. I've just spun it and it came up with 3 40's.
    Keith

    I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.

  12. #12
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: VB6 Lottery

    Well, I can't straight up give him everything, can I? If it's an assignment... What I posted is a solid base that knocks out the sorting, which is generally the roughest part of something like this. Adding that in will be a good learning experience for him.

  13. #13
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 2004
    Location
    Staffordshire, England
    Posts
    2,236

    Re: VB6 Lottery

    Thats Ok I didn't want him to think you had cracked it for him.
    Quote Originally Posted by Phurbu
    Thanks so much, exactly what I was looking for.
    Keith

    I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.

  14. #14
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: VB6 Lottery

    Wow, that is a lot of code! Why not just use a random number generator fixed to generate numbers between two limits?

    In other words, use this function:

    VB Code:
    1. Public Function intRandomBetween(ByVal lowerNum As Integer, ByVal higherNum As Integer) As Integer
    2.  
    3.     intRandomBetween = Int((higherNum - lowerNum + 1) * Rnd + lowerNum)
    4.  
    5. End Function

    and then put this in a button to generate the numbers, and assign them to 6 different labels:

    VB Code:
    1. Dim gennums As String
    2.     Dim lng As Long
    3.     Dim newnum, high, low As Integer
    4.    
    5.     high = 53
    6.     low = 1
    7.    
    8.     For lng = 1 To 6
    9.             Do
    10.             newnum = intRandomBetween(low, high)
    11.                 If Not InStr(gennums, "," & newnum & ",") > 0 Then gennums = gennums & newnum & ",": Exit Do
    12.             Loop
    13.     Next
    14.    
    15.     Label1.Caption = Split(gennums, ",")(0)
    16.     Label2.Caption = Split(gennums, ",")(1)
    17.     Label3.Caption = Split(gennums, ",")(2)
    18.     Label4.Caption = Split(gennums, ",")(3)
    19.     Label5.Caption = Split(gennums, ",")(4)
    20.     Label6.Caption = Split(gennums, ",")(5)
    Last edited by BrendanDavis; Dec 3rd, 2006 at 06:54 PM.

  15. #15
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: VB6 Lottery

    Here is a way to produce six, unique, random numbers.

    VB Code:
    1. Dim colRandom As New Collection
    2. Dim intTry As Integer
    3.  
    4. Randomize
    5.  
    6. On Error Resume Next
    7.  
    8. Do Until colRandom.Count = 6
    9.     ' Generate the random number
    10.     intTry = Int(Rnd * 16 + 1)
    11.     ' Attempt to add it to the collection along with a matching key
    12.     colRandom.Add intTry, CStr(intTry)
    13.     If Err.Number = 457 Then
    14.         ' The key is already in the collection so it isn't added again
    15.     End If
    16. Loop
    17.  
    18. For intTry = 1 To 6
    19.     Debug.Print colRandom(intTry)
    20. Next

  16. #16
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: VB6 Lottery

    If he's just learning, why make it so complicated? Generate a number, test it against the other numbers in the array, and if there's a match, generate a new number. Repeat process. Too easy.

  17. #17
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: VB6 Lottery

    Quote Originally Posted by timeshifter
    If he's just learning, why make it so complicated? Generate a number, test it against the other numbers in the array, and if there's a match, generate a new number. Repeat process. Too easy.
    That's what my code does

  18. #18
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: VB6 Lottery

    Quote Originally Posted by timeshifter
    If he's just learning, why make it so complicated? Generate a number, test it against the other numbers in the array, and if there's a match, generate a new number. Repeat process. Too easy.
    It's not complicated, and in fact it's quite straightforward.

  19. #19
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: VB6 Lottery

    Quote Originally Posted by BrendanDavis
    Wow, that is a lot of code! Why not just use a random number generator fixed to generate numbers between two limits?
    It's maybe a few lines of code more, but, what does a complete beginner (as he said he is) know about InStr(), Split()... timeshifter's code is much more... easy... for him - perhaps he understands it, not just copy-pastes it...

    It has few loops, few arrays and uses Rnd() which is the whole point of this app, and that's it... Quite simple and effective

  20. #20
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: VB6 Lottery

    Thank you somebody for seeing the point...

  21. #21
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: VB6 Lottery

    In addition:
    VB Code:
    1. Label1.Caption = Split(gennums, ",")(0)
    2. '...
    3. Label6.Caption = Split(gennums, ",")(5)
    ... is a bad practice. Create/split an array and loop through it... It's about 5 times faster with 1000 iterations and creates only 1 instead of 6 arrays with each iteration

  22. #22
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: VB6 Lottery

    Again, my code does that...

  23. #23
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: VB6 Lottery

    Quote Originally Posted by gavio
    In addition:
    VB Code:
    1. Label1.Caption = Split(gennums, ",")(0)
    2. '...
    3. Label6.Caption = Split(gennums, ",")(5)
    ... is a bad practice. Create/split an array and loop through it... It's about 5 times faster with 1000 iterations and creates only 1 instead of 6 arrays with each iteration
    I thought about doing that but I changed it last minute for the sake of time. I had to go christmas shopping, lol.

    As for being a beginner: nothing wrong with martin or I's code being too complex. Everything I used was basic VB knowledge that he should learn eventually, and it's not all that complex either. It's all a matter of how fast someone learns, really. I would rather someone give me the best (or one of the best) ways to do something right off the bat, and have them explain it to me, rather than show me a less efficient way to do something just because I'd understand it better with less explanation. But again, that's me and everyone's different.

  24. #24
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: VB6 Lottery

    Another alternative, which addresses the uniqe numbers issue, is to store all numers in an array, if there are sixteen numbers then create an array from index 0 to 15... on app start the array is initialized with values from 1 to 16 sorted, you now have a list of unique numbers... you then create a procedure that "shuffles" these sixteen numbers such as in http://www.vbforums.com/showthread.php?t=440551

    After the numbers have been shuffled, just get the first six numbers and show these in your lables. Every time you need to generate another set of random numbers, just call your "shuffle" procedure.

    Your sort procedure will work on the same array but will only sort the first 6 numbers ignoring the rest of the values.

    You end up working with only one data structure which makes everything easy.
    Last edited by leinad31; Dec 4th, 2006 at 01:45 AM.

  25. #25
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 2004
    Location
    Staffordshire, England
    Posts
    2,236

    Re: VB6 Lottery

    Quote Originally Posted by BrendanDavis
    VB Code:
    1. Dim gennums As String
    2.     Dim lng As Long
    3.     Dim newnum, high, low As Integer
    Only low will be an Integer, newnum and high will be Variants.
    VB Code:
    1. Dim newnum As Integer, high As Integer, low As Integer
    Keith

    I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.

  26. #26

  27. #27

    Thread Starter
    New Member
    Join Date
    Dec 2006
    Posts
    5

    Re: VB6 Lottery

    Wow. Thankyou so much for all of your input, if theres ever anything I can do to repay you or if I'm able to help out in anyway let me know. Not much of a programmer at the moment though, so can't help much in that area but in time I should be able to, especially thanks to all of your help.

    I'm going to try out some of things you all have mentioned and go over my program later on when I get back from the whole Xmas shopping thing lol.

    You all have deffinately proved much more help than my tutor, greatly appreciated. Thanks again.

  28. #28
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: VB6 Lottery

    You're welcome

    If you concider this to be resolved then please pull down the 'Thread Tools' menu and click 'Mark Thread Resolved' so people will know you got your answer

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