|
-
Dec 3rd, 2006, 03:03 PM
#1
Thread Starter
New Member
[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
-
Dec 3rd, 2006, 03:06 PM
#2
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.
-
Dec 3rd, 2006, 03:13 PM
#3
Thread Starter
New Member
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.
-
Dec 3rd, 2006, 03:23 PM
#4
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:
'...
If Values(counter) = lbl1.Caption Or Values(counter) = lbl2.Caption Or Values(counter) = lbl3.caption '...
'...
-
Dec 3rd, 2006, 03:44 PM
#5
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.
-
Dec 3rd, 2006, 03:48 PM
#6
Re: VB6 Lottery
 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.
-
Dec 3rd, 2006, 03:52 PM
#7
Thread Starter
New Member
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.
-
Dec 3rd, 2006, 04:04 PM
#8
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.
-
Dec 3rd, 2006, 04:31 PM
#9
Thread Starter
New Member
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.
-
Dec 3rd, 2006, 04:34 PM
#10
-
Dec 3rd, 2006, 06:19 PM
#11
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.
-
Dec 3rd, 2006, 06:30 PM
#12
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.
-
Dec 3rd, 2006, 06:34 PM
#13
Re: VB6 Lottery
Thats Ok I didn't want him to think you had cracked it for him. 
 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.
-
Dec 3rd, 2006, 06:51 PM
#14
Hyperactive Member
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:
Public Function intRandomBetween(ByVal lowerNum As Integer, ByVal higherNum As Integer) As Integer
intRandomBetween = Int((higherNum - lowerNum + 1) * Rnd + lowerNum)
End Function
and then put this in a button to generate the numbers, and assign them to 6 different labels:
VB Code:
Dim gennums As String
Dim lng As Long
Dim newnum, high, low As Integer
high = 53
low = 1
For lng = 1 To 6
Do
newnum = intRandomBetween(low, high)
If Not InStr(gennums, "," & newnum & ",") > 0 Then gennums = gennums & newnum & ",": Exit Do
Loop
Next
Label1.Caption = Split(gennums, ",")(0)
Label2.Caption = Split(gennums, ",")(1)
Label3.Caption = Split(gennums, ",")(2)
Label4.Caption = Split(gennums, ",")(3)
Label5.Caption = Split(gennums, ",")(4)
Label6.Caption = Split(gennums, ",")(5)
Last edited by BrendanDavis; Dec 3rd, 2006 at 06:54 PM.
-
Dec 3rd, 2006, 06:59 PM
#15
Re: VB6 Lottery
Here is a way to produce six, unique, random numbers.
VB Code:
Dim colRandom As New Collection
Dim intTry As Integer
Randomize
On Error Resume Next
Do Until colRandom.Count = 6
' Generate the random number
intTry = Int(Rnd * 16 + 1)
' Attempt to add it to the collection along with a matching key
colRandom.Add intTry, CStr(intTry)
If Err.Number = 457 Then
' The key is already in the collection so it isn't added again
End If
Loop
For intTry = 1 To 6
Debug.Print colRandom(intTry)
Next
-
Dec 3rd, 2006, 07:08 PM
#16
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.
-
Dec 3rd, 2006, 07:13 PM
#17
Hyperactive Member
Re: VB6 Lottery
 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
-
Dec 3rd, 2006, 07:15 PM
#18
Re: VB6 Lottery
 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.
-
Dec 3rd, 2006, 07:23 PM
#19
Re: VB6 Lottery
 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
-
Dec 3rd, 2006, 07:30 PM
#20
Re: VB6 Lottery
Thank you somebody for seeing the point...
-
Dec 3rd, 2006, 07:41 PM
#21
Re: VB6 Lottery
In addition:
VB Code:
Label1.Caption = Split(gennums, ",")(0)
'...
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
-
Dec 3rd, 2006, 07:59 PM
#22
Re: VB6 Lottery
Again, my code does that...
-
Dec 3rd, 2006, 08:48 PM
#23
Hyperactive Member
Re: VB6 Lottery
 Originally Posted by gavio
In addition:
VB Code:
Label1.Caption = Split(gennums, ",")(0)
'...
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.
-
Dec 4th, 2006, 01:26 AM
#24
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.
-
Dec 4th, 2006, 07:48 AM
#25
Re: VB6 Lottery
 Originally Posted by BrendanDavis
VB Code:
Dim gennums As String
Dim lng As Long
Dim newnum, high, low As Integer
Only low will be an Integer, newnum and high will be Variants.
VB Code:
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.
-
Dec 4th, 2006, 08:12 AM
#26
Re: VB6 Lottery
Exactly. I think this behavior has been fixed in .NET.
-
Dec 4th, 2006, 09:01 AM
#27
Thread Starter
New Member
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.
-
Dec 4th, 2006, 09:05 AM
#28
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|