Results 1 to 5 of 5

Thread: [VB6] - Lottery-Algorithm

Threaded View

  1. #1

    Thread Starter
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,262

    [VB6] - Lottery-Algorithm

    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

    EDIT: cleaned up Version: https://www.vbforums.com/showthread....=1#post5556369

    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)
    Last edited by Zvoni; Feb 16th, 2022 at 03:05 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width