Results 1 to 10 of 10

Thread: random shuffle

Threaded View

  1. #2
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    There are many ways to do what I believe you're suggesting.
    The fastest, easiest way, for me is something like the following:

    VB Code:
    1. Dim I_WAS_SELECTED() As Boolean
    2. Dim SelCount As Integer
    3. Dim MyUpper As Integer
    4.  
    5. Private Sub Command1_Click()
    6. Randomize
    7. Dim SELECTED_1_Thru_MyUpper  As Integer '
    8.     If SelCount = MyUpper  Then
    9.         Call REDIM_IT
    10.         MsgBox "Starting Over Again!"
    11.     End If
    12.    
    13.     SELECTED_1_Thru_MyUpper  = Int(Rnd * MyUpper )
    14.     While I_WAS_SELECTED(SELECTED_1_Thru_MyUpper ) = True
    15.         SELECTED_1_Thru_MyUpper  = (SELECTED_1_Thru_MyUpper  + 1) Mod MyUpper
    16.     Wend
    17.     SelCount = SelCount + 1
    18.     I_WAS_SELECTED(SELECTED_1_Thru_MyUpper ) = True
    19.     SELECTED_1_Thru_MyUpper  = SELECTED_1_Thru_MyUpper  + 1     '0 thru 14 equiv to 1 thru 15
    20.     MsgBox "Number " & SelCount & " Is " & (SELECTED_1_Thru_MyUpper )
    21.  
    22. End Sub
    23.  
    24. Private Sub Form_Load()
    25. MyUpper = 15
    26. Call REDIM_IT
    27. End Sub
    28. Private Sub REDIM_IT()
    29.         ReDim I_WAS_SELECTED(MyUpper - 1)
    30.         SelCount = 0
    31. End Sub

    As you can see, an array dimmed as boolean keeps track of whats been chosen. When a random selection is made, if that has been chosen already, the number is incrimented modularly, until an unchosen number is found.

    Also, if all the numbers have been chosen, then you reset the arrays and the counter, and start all over again.

    However, this is NOT the recommended way to do it, since it still is a time consuming process.

    I believe Guv, Jim Macnemara {sp?}, MartinLiss or Plenderj have a good "shuffling" system somewhere around here.

    And, I have a way useing copymem to remove elements from an array, without leaving holes, and then reinserting them back in the original order, which is definitely applicable to this problem.

    -Lou

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