I wrote this class a few years back in vb.net 2005, and I have migrated it to work in 2013. This class is for randomly picking Bingo balls. It works by placing 75 numbers in an array, and them randomly swapping the numbers 100,000 times when the class in instantiate. When the getBall method is called, the class shuffles the balls 10,000 more times before returning a number from a random location.
In all, the balls are shuffled 850,000 times — assuming all 75 balls are called.
Public Class BingoBalls
Private BallArray() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, _
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, _
31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, _
46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, _
61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75}
Private BallCount As Integer = 0
Public Sub New()
For cnt As Integer = 1 To 100000
Shuffle(BallArray)
Next
End Sub
Public Function reset() As Boolean
BallArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, _
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, _
31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, _
46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, _
61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75}
BallCount = 0
For cnt As Integer = 1 To 100000
Shuffle(BallArray)
Next
Return True
End Function
Public Function getBall() As Integer
BallCount = BallCount + 1
If BallCount < 76 Then
For cnt As Integer = 1 To 10000
Shuffle(BallArray)
Next
Dim randomizer As New Random
Dim Position As Integer
Dim rand_ball As Integer = 0
Do While rand_ball = 0
Position = randomizer.Next(0, BallArray.Length - 1)
If Not BallArray(Position) = 0 Then
rand_ball = BallArray(Position)
BallArray(Position) = 0
End If
Loop
Return rand_ball
End If
Return 0
End Function
Private Sub Shuffle(ByRef BingoBallArray() As Integer)
Dim counter As Integer
Dim newPosition As Integer
Dim shuffleMethod As New Random
Dim tempObject As Object
For counter = 0 To BingoBallArray.Length - 1
newPosition = shuffleMethod.Next(0, BingoBallArray.Length - 1)
tempObject = BingoBallArray(counter)
BingoBallArray(counter) = BingoBallArray(newPosition)
BingoBallArray(newPosition) = tempObject
Next counter
End Sub
End Class