Introduction
If you are one of the many unlucky ones who has never won anything in a lottery, well, this article is for you. Because I didn’t win the lottery so that I could live a more comfortable life without stress and being tired all the time because of the three jobs I do, I decided to see if I can create a basic lottery type game. At least I can always win with my game, and it is free to play…
Let’s get right into it; there is actually quite a lot of work.
Design
Open Visual Studio and create a new Visual Basic.NET Windows Forms project. Add panels, timers, and buttons to your form so that it resembles Figure 1.

Figure 1: Design
Let’s move on to the code.
The Code
Add a new Class and name it clsShuffle, then add the next code in it.
Public Class clsShuffle
Const intSize As Integer = 20
Public intMin As Integer
Public intMax As Integer
Private dbSize(intSize - 1) As Double
Private intThird As Integer
Private intHalf As Integer
Public dbRand As Double
Public intRand As Integer
Public Sub Shuffle(ByVal dbVal As Double)
Dim stVal As String
Dim i As Integer
Take()
stVal = Str$(dbVal)
For i = 1 To Len(stVal)
Mix(1 / Asc(Mid$(stVal, i, 1)))
Next
Randomize(Rnd(dbSize(intThird) * Math.Sign(dbVal)))
For i = 1 To intSize * 2.5
Mix(Rnd())
Next
End Sub
Public ReadOnly Property RandomDouble() As Double
Get
intThird = (intThird + 1) Mod intSize
intHalf = (intHalf + 1) Mod intSize
dbSize(intThird) += dbSize(intThird) + Rnd()
dbSize(intThird) = dbSize(intThird) - _
Int(dbSize(intThird))
dbRand = dbSize(intThird)
Return dbRand
End Get
End Property
Public ReadOnly Property RandomInt() As Integer
Get
intRand = Int(RandomDouble() * intMax - intMin + 1) + _
intMin
Return intRand
End Get
End Property
Private Sub Take()
Dim i As Integer
For i = 1 To intSize - 1
dbSize(i) = 1 / i
Next
intThird = intSize / 2
intHalf = intSize / 3
If intThird = intHalf Then
intThird = intThird + 1
End If
End Sub
Private Sub Mix(ByVal dbVal As Double)
intThird = (intThird + 1) Mod intSize
intHalf = (intHalf + 1) Mod intSize
dbSize(intThird) += dbSize(intThird) + dbVal
dbSize(intThird) = dbSize(intThird) - Int(dbSize(intThird))
End Sub
End Class
On the form, add the following variables:
Const iMax As Integer = 56
Const iPowerMax As Integer = 46
Private blnShow As Boolean = False
Dim Balls(50)
Dim PowerBalls(40)
Dim cShuffleBalls As New clsShuffle
Dim cShufflePowerBalls As New clsShuffle
Dim intPowerBall As Integer
Add the Form Load event that starts the application:
Private Sub frmLotto_Load(sender As Object, e As _
System.EventArgs) Handles Me.Load
Randomize()
cShuffleBalls.Shuffle(Rnd)
cShufflePowerBalls.Shuffle(Rnd)
cShuffleBalls.intMin = 1
cShuffleBalls.intMax = iMax
cShufflePowerBalls.intMin = 1
cShufflePowerBalls.intMax = iPowerMax
tmrBalls.Start()
End Sub
This sets all the default properties. Add the Timer Tick events for both Timers.
Private Sub tmrBalls_Tick(sender As Object, e As _
System.EventArgs) Handles tmrBalls.Tick
blnShow = True
pnlDrum.Invalidate()
End Sub
Private Sub tmrPower_Tick(sender As Object, e As _
System.EventArgs) Handles tmrPower.Tick
blnShow = True
pnlPowerDrum.Invalidate()
End Sub
This determines if the normal balls’ panel should show or only the Powerball’s panel. Add the code for the buttons:
Private Sub btnNext_Click(sender As Object, e As _
System.EventArgs) Handles btnNext.Click
If btnNext.Text = "Play again" Then
btnNext.Text = "Next Ball"
tmrBalls.Start()
End If
Dim i As Integer
i = Balls(0)
If i = 5 Then
For i = 0 To 5
Balls(i) = 0
Next
Exit Sub
End If
NextBall(Balls)
If Balls(0) = 5 Then
btnNext.Text = "Play again"
tmrBalls.Stop()
pnlDrum.Hide()
pnlPowerDrum.Show()
tmrPower.Start()
End If
End Sub
Private Sub btnClearAll_Click(sender As System.Object, e As _
System.EventArgs) Handles btnClear.Click
ClearBalls()
End Sub
Private Sub btnPowerBall_Click(sender As Object, e As _
System.EventArgs) Handles btnPowerBall.Click
NextPowerBall(PowerBalls)
tmrBalls.Stop()
pnlDrum.Show()
pnlPowerDrum.Hide()
tmrPower.Stop()
End Sub
The Next Ball button picks another ball. The Powerball button picks the Powerball. The Clear button clears all the drawings from all the panels.
Add the ball picking sub procedures:
Private Sub NextBall(ByVal tmpArray())
Dim i As Integer
Dim j As Integer = 0
tmpArray(0) = tmpArray(0) + 1
i = tmpArray(0)
Do
tmpArray(i) = cShuffleBalls.RandomInt
Display(tmpArray(i))
If i > 1 Then
For j = 1 To (i - 1)
If tmpArray(i) = tmpArray(j) Then
tmpArray(i) = 0
End If
Next
End If
Loop Until tmpArray(i)
End Sub
Private Sub NextPowerBall(ByVal arrTemp())
Dim i As Integer
Dim j As Integer = 0
arrTemp(0) = arrTemp(0) + 1
i = arrTemp(0)
Do
arrTemp(i) = cShuffleBalls.RandomInt
DisplayPowerBall(arrTemp(i))
If i > 1 Then
For j = 1 To (i - 1)
If arrTemp(i) = arrTemp(j) Then
arrTemp(i) = 0
End If
Next
End If
Loop Until arrTemp(i)
End Sub
Private Sub DisplayPowerBall(ByVal intVal As Integer)
intPowerBall = intVal
Select Case intPowerBall
Case 1 To 40
blnShow = True
pnlPowerBall.Invalidate()
End Select
End Sub
The sub procedures randomize the next ball number with the help of the Shuffle class and picks a ball to display. This is where things get interesting. We need to display the balls somewhere. Now, because we have so many panels, it will be extremely difficult to post all the code here, that is why I have put it on GitHub here.
Let’s add the panels for the shuffling of the balls, as well as the panel for displaying some of the balls.
Private Sub pnlTumble_Paint(sender As Object, e As _
System.Windows.Forms.PaintEventArgs) Handles pnlDrum.Paint
Dim i As Integer = 0
Dim intX As Integer
Dim intY As Integer
Dim rRand As New Random
Dim rctDraw As New Rectangle
For i = 1 To 50
intX = rRand.Next(1, 300)
intY = rRand.Next(1, 400)
rctDraw.Height = 50
rctDraw.Width = 50
rctDraw.Location = New Point(intX, intY)
e.Graphics.FillEllipse(Brushes.White, rctDraw.X, _
rctDraw.Y, 50, 50)
Dim fDraw As Font = New Font("Arial", 10, FontStyle.Bold)
Dim StringSize As SizeF = _
e.Graphics.MeasureString(i.ToString(), fDraw)
e.Graphics.DrawString(i.ToString(), fDraw, Brushes.Black, _
(rctDraw.Left + 25) - (StringSize.Width / 2), _
(rctDraw.Top + 25) - (StringSize.Height / 2))
Next
e.Graphics.Clear(Color.Black)
e.Graphics.Dispose()
End Sub
Private Sub pnlPowerDrum_Paint(sender As Object, e As _
System.Windows.Forms.PaintEventArgs) Handles _
pnlPowerDrum.Paint
Dim i As Integer = 0
Dim xPos As Integer
Dim yPos As Integer
Dim rr As New Random
Dim rect As New Rectangle
For i = 1 To 40
xPos = rr.Next(1, 300)
yPos = rr.Next(1, 400)
rect.Height = 50
rect.Width = 50
rect.Location = New Point(xPos, yPos)
e.Graphics.FillEllipse(Brushes.Yellow, rect.X, rect.Y, _
50, 50)
Dim f As Font = New Font("Arial", 12, FontStyle.Bold)
Dim StringSize As SizeF = _
e.Graphics.MeasureString(i.ToString(), f)
e.Graphics.DrawString(i.ToString(), f, Brushes.Black, _
(rect.Left + 25) - (StringSize.Width / 2), _
(rect.Top + 25) - (StringSize.Height / 2))
Next
e.Graphics.Clear(Color.Black)
e.Graphics.Dispose()
End Sub
Private Sub pnlPowerBall_Paint(sender As Object, e As _
System.Windows.Forms.PaintEventArgs) Handles _
pnlPowerBall.Paint
If blnShow Then
Dim rect As New Rectangle
rect.Height = 93
rect.Width = 93
rect.Location = New Point(0, 0)
e.Graphics.FillEllipse(Brushes.Yellow, rect.X, rect.Y, _
93, 93)
Dim strPowerBall As String = "Power Ball"
Dim t As String = intPowerBall.ToString
Dim f As Font = New Font("Arial", 16, FontStyle.Bold)
Dim StringSize As SizeF = e.Graphics.MeasureString(t, f)
e.Graphics.DrawString(t, f, Brushes.Black, _
(rect.Width / 2) - (StringSize.Width / 2), _
(rect.Height / 2) - (StringSize.Height / 2))
Dim fmb As Font = New Font("Arial", 10, FontStyle.Bold)
Dim mbStringSize As SizeF = _
e.Graphics.MeasureString(strPowerBall, fmb)
e.Graphics.DrawString(strPowerBall, fmb, Brushes.Black, _
(rect.Left + 47) - (mbStringSize.Width / 2), _
(rect.Top + 18) - (mbStringSize.Height / 2))
e.Graphics.Dispose()
End If
End Sub
This shuffles all the balls and displays the power ball.
The logic to clear all the balls as well as paint each ball is present in the project on GitHub, here.
Conclusion
I hope you have enjoyed this article. Yes, it was a lot of work and it could have been done much better with much less code, but this enables newbies to also follow the logic properly.