1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| Option Explicit
Public Function Shuffle(Deck As Variant) As Variant
Dim intUBound As Integer
Dim intHUBound As Integer
Dim intRanNum As Integer
Dim a As Variant
Dim b As Variant
Dim i As Integer
Dim blnError As Boolean
'Starts the randon deck Shuffle
intUBound = UBound(Deck)
intHUBound = intUBound
ReDim a(intUBound)
ReDim b(intUBound)
For i = 0 To intUBound
a(i) = Deck(i) ' Sets array (a) list to (1 to cntnum)
Next i
Do
Randomize ' Activates the Random Number Generator
intRanNum = Int(Rnd() * intUBound) ' Picks a Random number between 0 and max number
b(0) = a(intRanNum)
On Error GoTo ErrCritique
For i = 1 To intUBound ' Starts a loop
If intRanNum = intUBound Then intRanNum = -1 ' If the Random number = max number then the Random number becomes 0
intRanNum = intRanNum + 1 ' Adds 1 to the Random number
b(i) = a(intRanNum) ' b(current loop value) = The current Random number
Next i ' Adds 1 to i and loops until i is greater than the max Number
On Error GoTo 0
For i = 0 To intUBound
a(i) = b(i) ' Makes array a the same as array b
Next i
intUBound = intUBound - 1 ' Subtracts 1 from the max number
Loop Until intUBound = -1
Shuffle = a
Exit Function
ErrCritique:
MsgBox "An Error has occured, Outcome may not be acurate", vbCritical, "Error"
End Function
Private Sub Command1_Click()
Dim Tble As Variant
Dim Resultat As Variant
Dim iPnt As Integer
Tble = Split("0,1,2,3,4,5,6,7,8,9,10,11", ",")
Resultat = Shuffle(Tble)
For iPnt = 0 To 11
Debug.Print iPnt, Resultat(iPnt)
Next
End Sub |
Partager