I recently wrote a bit of code that randomized numbers that was stored in an array. The code worked by picking two random locations in the array and swapping the values in those locations. Then, I wrote a code that would pick a random location and return the value.
The random numbers that were picked for swapping value in the array were the same number, so the array wasn’t thoroughly randomized, even if I called the swapping method hundreds of thousands of times. As a work-a-around, I rewrote the swapping function to loop over the array, and replace the current iteration with a random location. This insured that every number was touched, and the chances of randomly picking the same number of the iteration were slim.
Below is the code:
Private Sub Shuffle(ByVal BingoBallArray() As Integer) Static RandGen As System.Random = New System.Random(System.DateTime.Now.Millisecond) Dim temp As Integer For cnt As Integer = 0 To (BingoBallArray.Length - 1) Dim ArrayPoistion As Integer = RandGen.Next(0,
(BingoBallArray.Length - 1)) temp = BingoBallArray(cnt) BingoBallArray(cnt) = BingoBallArray(ArrayPoistion) BingoBallArray(ArrayPoistion) = temp Next End Sub