Lotto Numbers Generator

The example is an expansion of the Lotto Number Generator program.  It allows users to select their inputs, number of population set (from 10 to 100) and the number of data point to be selected (from 2 to 10), and then populate the random number.  Even though the lotto example is used, gambling is not encouraged.

This program involves resampling without replacement. The sub-procedure, DoubleSort( ), sorts the numbers of one array based on the numbers of another array. The sub-procedure, Resample( ), creates numbers from 1 to 54 for array two (Hold2) and 54 random numbers for array one (Hold), and calls in the sub-procedure, DoubleSort( ). When executed,the program will return 5 sets of 6 lotto numbers in column B through F starting from row 13 of the worksheet. The user can select the number of sets by altering the number of iterations on line 4 of the program. An example of the output is displayed below.


Option Explicit
Option Base 1
Public jj As Long
Public Const iteration = 5

'*                                     Resampling Process                                       *

Sub Resample()
    Dim i As Long
    Dim hold(54) As Single, Hold2(54) As Single

    For i = 1 To 54
              Hold2(i) = i
    Next i

    For jj = 1 To iteration

        For i = 1 To 54

            hold(i) = Rnd
        Next i

        Call DoubleSort(54, hold, Hold2)

        For i = 1 To 6

            Cells(jj + 3, i) = Hold2(i)
        Next i
   Next jj

End Sub

'*             Sorting Process -  Sort array y based on array x                 *

Sub DoubleSort(n As Long, x() As Single, y() As Single)
    Dim xTemp As Double
    Dim yTemp As Double
    Dim i As Long
    Dim j As Long

    For j = 2 To n

        xTemp = x(j)

        yTemp = y(j)

            For i = j - 1 To 1 Step -1

                If (x(i) <= xTemp) Then GoTo 10
                x(i + 1) = x(i)
                y(i + 1) = y(i)
            Next i

        i = 0

10   x(i + 1) = xTemp
        y(i + 1) = yTemp

    Next j

End Sub

VBA Codes