Computing.Net > Forums > Programming > lottery program (can u revise it)

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

lottery program (can u revise it)

Reply to Message Icon

Name: themodel
Date: March 4, 2005 at 14:02:38 Pacific
OS: XP Home SP1
CPU/Ram: 256
Comment:

I'm just starting out in Visual Basic 6 and wrote a lottery generator program which selects 6 random numbers (between 1 and 49) and then is displayed to the user.
I know this is bad coding, but you all write some alternatives to the wy i coded it, ie better loops and shorter code.

Thanks (my code is as follows)

Private Sub Command1_Click()
Label2.Visible = False
Label3.Visible = False
Label4.Visible = False
Label5.Visible = False
Label6.Visible = False
Label7.Visible = False
Label9.Visible = False
Randomize (Timer) ' Seed Randomiser
Label2.Caption = Int(Rnd * 50)
Do While Label2.Caption = 0
Label2.Caption = Int(Rnd * 50)
Loop
Label2.Visible = True
Label3.Caption = Int(Rnd * 50)
Do While Label3.Caption = Label2.Caption Or Label3.Caption = 0
Label3.Caption = Int(Rnd * 50)
Loop
Label3.Visible = True
Label4.Caption = Int(Rnd * 50)
Do While Label4.Caption = Label3.Caption Or Label4.Caption = Label2.Caption Or Label4.Caption = 0
Label4.Caption = Int(Rnd * 50)
Loop
Label4.Visible = True
Label5.Caption = Int(Rnd * 50)
Do While Label5.Caption = Label4.Caption Or Label5.Caption = Label3.Caption Or Label5.Caption = Label2.Caption Or Label5.Caption = 0
Label5.Caption = Int(Rnd * 50)
Loop
Label5.Visible = True
Label6.Caption = Int(Rnd * 50)
Do While Label6.Caption = Label5.Caption Or Label6.Caption = Label4.Caption Or Label6.Caption = Label3.Caption Or Label6.Caption = Label2.Caption Or Label6.Caption = 0
Label6.Caption = Int(Rnd * 50)
Loop
Label6.Visible = True
Label7.Caption = Int(Rnd * 50)
Do While Label7.Caption = Label6.Caption Or Label7.Caption = Label5.Caption Or Label7.Caption = Label4.Caption Or Label7.Caption = Label3.Caption Or Label7.Caption = Label2.Caption Or Label7.Caption = 0
Label7.Caption = Int(Rnd * 50)
Loop
Label7.Visible = True
Label9.Visible = True
Command1.Caption = "Generate Again"
End Sub



Sponsored Link
Ads by Google

Response Number 1
Name: StuartS
Date: March 4, 2005 at 15:34:13 Pacific
Reply:

Use a control array

Randomize (Timer)
For a = 0 to 8
label(a) = Int(Rnd * 50)
label(a).visible = true
next

Noe all you have to do is to devise a method of ensuring duplicate numbers are discarded. It can all be done within the For Next loop.

I better way would be a loop within the loop to check the contents of each label and discards the number if a match is found.

A lost faster then the = Or construction.

Stuart


0

Response Number 2
Name: HiJinx
Date: March 4, 2005 at 16:58:27 Pacific
Reply:

The code below assumes a single label. You can rewrite that part if you want to keep mulitple labels (as suggested, making an array of them would be better).

The code chooses a random element from within the lotto array, then replaces its value with the last element's and adjusts the range of elements to choose from the next time a random number is chosen. This prevents you from choosing the same number twice.

The code also uses a simple bubble sort to sort the chosen numbers.

======================================================

Private Sub Command1_Click()

Dim x As Integer
Dim y As Integer
Dim z As Integer
Dim last As Integer
Dim temp As Integer

Dim numbers(5) As Integer
Dim lotto(49) As Integer

Randomize (Timer)
last = 49
Label1.Caption = ""

For x = 0 To 49
lotto(x) = x
Next

For x = 0 To 5
z = Int(Rnd * last + 1)
numbers(x) = lotto(z)
lotto(z) = lotto(last)
last = last - 1
Next

'Sort array using simple bubble sort

For x = 0 To UBound(numbers) - 1
For y = 0 To UBound(numbers) - 1
If numbers(y) > numbers(y + 1) Then
temp = numbers(y)
numbers(y) = numbers(y + 1)
numbers(y + 1) = temp
End If
Next y
Next x

For x = 0 To 5
Label1.Caption = Label1.Caption + Str(numbers(x)) + " "
Next


End Sub



0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: lottery program (can u revise it)

Big big problem can u solve it? www.computing.net/answers/programming/big-big-problem-can-u-solve-it/3961.html

WERE CAN U LEARN www.computing.net/answers/programming/were-can-u-learn-/1976.html

c programming help needed www.computing.net/answers/programming/c-programming-help-needed/10441.html