Results 1 to 5 of 5

Thread: Card Shuffling [SOLVED]

  1. #1

    Thread Starter
    Fanatic Member hothead's Avatar
    Join Date
    Mar 2002
    Location
    Missouri
    Posts
    692

    Card Shuffling [SOLVED]

    How would one go about shuffling an array of 40 integers? I've tried several examples, but either I'm not doing them properly or they just don't work.

    BTW: I'm not doing this directly within the form, but from inside a class module... I'm trying to do as little in-form coding as possible, in the hopes that it might make my programs run a little faster.
    Last edited by hothead; Oct 2nd, 2003 at 05:02 PM.

  2. #2
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    Working on it...
    Please rate my post.

  3. #3
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.  
    5.     Dim intArray(39) As Integer, intShuffled(39) As Integer
    6.     Dim intFN As Integer
    7.    
    8.    
    9.     Dim strShow As String
    10.    
    11.     For intFN = 0 To 39
    12.         intArray(intFN) = intFN
    13.     Next intFN
    14.    
    15.     Call ShuffleArray(intArray(), intShuffled())
    16.    
    17.     For intFN = 0 To 39
    18.         strShow = strShow & intFN & ") " & intShuffled(intFN) & vbTab & intArray(intFN) & vbCrLf
    19.     Next intFN
    20.        
    21.     MsgBox strShow
    22.    
    23. End Sub
    24.  
    25. Private Function ShuffleArray(ByRef intArray() As Integer, ByRef intReturnArray() As Integer) As Integer
    26.  
    27.     Dim intExclude() As Integer
    28.     Dim intUBound As Integer, intFN As Integer, intAssigningValue As Integer, intIndex As Integer
    29.    
    30.     intUBound = UBound(intArray)
    31.     ReDim intRetVal(intUBound)
    32.     ReDim intExclude(intUBound)
    33.    
    34.     For intFN = 0 To intUBound
    35.         intExclude(intFN) = -1
    36.     Next intFN
    37.    
    38.     For intFN = 0 To intUBound
    39.         intAssigningValue = intArray(intFN)
    40.         intIndex = RandomInteger(0, intUBound + 1, intExclude())
    41.        
    42.         intReturnArray(intIndex) = intAssigningValue
    43.         intExclude(intFN) = intIndex
    44.        
    45.         Debug.Print intIndex
    46.    
    47.     Next intFN
    48.    
    49. End Function
    50.  
    51. Private Function RandomInteger(ByVal intMinimum As Integer, _
    52.                             ByVal intMaximum As Integer, _
    53.                             intExcludeValues() As Integer) As Integer
    54.  
    55.     Dim intFN As Integer, blnFound As Boolean, intExcludeValue As Integer
    56.    
    57.     Randomize
    58.     blnFound = False
    59.    
    60.     Do
    61.         DoEvents
    62.         RandomInteger = Int(Rnd * (intMaximum - intMinimum)) + intMinimum
    63.        
    64.         For intFN = 0 To UBound(intExcludeValues())
    65.             intExcludeValue = intExcludeValues(intFN)
    66.             If RandomInteger = intExcludeValue Then
    67.                 GoTo lblNotFound
    68.             End If
    69.         Next intFN
    70.        
    71.         blnFound = True
    72. lblNotFound:
    73.     Randomize
    74.     Loop Until blnFound = True
    75.    
    76. End Function

    I'd be happy to explain the coding if you don't understand it. I don't usually comment on sample coding.
    Please rate my post.

  4. #4

    Thread Starter
    Fanatic Member hothead's Avatar
    Join Date
    Mar 2002
    Location
    Missouri
    Posts
    692
    Thanks for the response. I shall try this once I get off work tonight, as I don't get much time to code in the mornings.

  5. #5

    Thread Starter
    Fanatic Member hothead's Avatar
    Join Date
    Mar 2002
    Location
    Missouri
    Posts
    692
    That helped. Thanks.
    Last edited by hothead; Oct 2nd, 2003 at 05:02 PM.

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