On a german Forum I developed the following Lottery-Algorithm (which i haven't found on the Internet in this form).
I would like to hear your opinions and/or suggestions to improve it
vb Code:
Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Sub Lottery(ByVal DrawNumbers As Long, ByVal TotalNumbers As Long)
Dim arrSource() As Long
Dim arrDest() As Long
Dim i As Long
Dim j As Long
Dim Counter As Long
Dim RandomNumber As Long
'What Lottery is played
ReDim arrDest(1 To DrawNumbers)
ReDim arrSource(1 To TotalNumbers)
'Create Source-Array
For i = 1 To TotalNumbers
arrSource(i) = i
Next
Counter = 0
Randomize
Do
RandomNumber = Int(UBound(arrSource) * Rnd + 1)
Counter = Counter + 1
arrDest(Counter) = arrSource(RandomNumber)
'Cutting out the RandomNumber drawn
For j = RandomNumber + 1 To UBound(arrSource)
CopyMemory arrSource(j - 1), arrSource(j), 4
Next
'Cut down the Source-Array
ReDim Preserve arrSource(1 To UBound(arrSource) - 1)
Loop Until Counter = DrawNumbers
For i=1 to DrawNumbers
Debug.Print arrDest(i)
Next
End Sub
'Calling the function with
Call Lottery (6, 49)