Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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

Use a control array
Randomize (Timer)
For a = 0 to 8
label(a) = Int(Rnd * 50)
label(a).visible = true
nextNoe 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

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 IntegerDim numbers(5) As Integer
Dim lotto(49) As IntegerRandomize (Timer)
last = 49
Label1.Caption = ""For x = 0 To 49
lotto(x) = x
NextFor 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

![]() |
![]() |
![]() |

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