Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1461

[VB6] - Lottery-Algorithm

$
0
0
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:
  1. Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
  2.  
  3. Sub Lottery(ByVal DrawNumbers As Long, ByVal TotalNumbers As Long)
  4. Dim arrSource() As Long    
  5. Dim arrDest() As Long
  6.  
  7. Dim i As Long
  8. Dim j As Long
  9. Dim Counter As Long
  10. Dim RandomNumber As Long
  11.  
  12.     'What Lottery is played
  13.     ReDim arrDest(1 To DrawNumbers)
  14.     ReDim arrSource(1 To TotalNumbers)
  15.    
  16.     'Create Source-Array
  17.     For i = 1 To TotalNumbers
  18.    
  19.         arrSource(i) = i
  20.    
  21.     Next
  22.    
  23.     Counter = 0
  24.  
  25.     Randomize
  26.  
  27.     Do
  28.  
  29.         RandomNumber = Int(UBound(arrSource) * Rnd + 1)
  30.    
  31.         Counter = Counter + 1
  32.         arrDest(Counter) = arrSource(RandomNumber)
  33.        
  34.         'Cutting out the RandomNumber drawn
  35.         For j = RandomNumber + 1 To UBound(arrSource)
  36.        
  37.             CopyMemory arrSource(j - 1), arrSource(j), 4
  38.            
  39.         Next
  40.        
  41.         'Cut down the Source-Array
  42.         ReDim Preserve arrSource(1 To UBound(arrSource) - 1)
  43.            
  44.     Loop Until Counter = DrawNumbers
  45.    
  46.     For i=1 to DrawNumbers
  47.      
  48.          Debug.Print arrDest(i)
  49.  
  50.     Next
  51.  
  52. End Sub
  53.  
  54. 'Calling the function with
  55. Call Lottery (6, 49)

Viewing all articles
Browse latest Browse all 1461

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>