|
-
Oct 1st, 2003, 11:17 PM
#1
Thread Starter
Fanatic Member
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.
-
Oct 2nd, 2003, 12:52 AM
#2
Frenzied Member
-
Oct 2nd, 2003, 01:30 AM
#3
Frenzied Member
VB Code:
Option Explicit
Private Sub Form_Load()
Dim intArray(39) As Integer, intShuffled(39) As Integer
Dim intFN As Integer
Dim strShow As String
For intFN = 0 To 39
intArray(intFN) = intFN
Next intFN
Call ShuffleArray(intArray(), intShuffled())
For intFN = 0 To 39
strShow = strShow & intFN & ") " & intShuffled(intFN) & vbTab & intArray(intFN) & vbCrLf
Next intFN
MsgBox strShow
End Sub
Private Function ShuffleArray(ByRef intArray() As Integer, ByRef intReturnArray() As Integer) As Integer
Dim intExclude() As Integer
Dim intUBound As Integer, intFN As Integer, intAssigningValue As Integer, intIndex As Integer
intUBound = UBound(intArray)
ReDim intRetVal(intUBound)
ReDim intExclude(intUBound)
For intFN = 0 To intUBound
intExclude(intFN) = -1
Next intFN
For intFN = 0 To intUBound
intAssigningValue = intArray(intFN)
intIndex = RandomInteger(0, intUBound + 1, intExclude())
intReturnArray(intIndex) = intAssigningValue
intExclude(intFN) = intIndex
Debug.Print intIndex
Next intFN
End Function
Private Function RandomInteger(ByVal intMinimum As Integer, _
ByVal intMaximum As Integer, _
intExcludeValues() As Integer) As Integer
Dim intFN As Integer, blnFound As Boolean, intExcludeValue As Integer
Randomize
blnFound = False
Do
DoEvents
RandomInteger = Int(Rnd * (intMaximum - intMinimum)) + intMinimum
For intFN = 0 To UBound(intExcludeValues())
intExcludeValue = intExcludeValues(intFN)
If RandomInteger = intExcludeValue Then
GoTo lblNotFound
End If
Next intFN
blnFound = True
lblNotFound:
Randomize
Loop Until blnFound = True
End Function
I'd be happy to explain the coding if you don't understand it. I don't usually comment on sample coding.
-
Oct 2nd, 2003, 06:41 AM
#4
Thread Starter
Fanatic Member
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.
-
Oct 2nd, 2003, 04:46 PM
#5
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|