Fisher Yates Shuffle for cards or any number of items
#2
(06-19-2023, 11:19 PM)bplus Wrote: As discussed in Ranking Poker Hands:
Code: (Select All)
TopN = 52
ReDim n(1 To TopN) 'repeatable for ref
For i = 1 To TopN
    n(i) = i
Next
For i = TopN To 2 Step -1 ' Fisher Yates Shuffle of N Items
    Swap n(i), n(Int(Rnd * (i) + 1))
Next
For i = 1 To TopN
    Print "  "; i; "-"; n(i); Chr$(9);
Next
Print

At maximum you need only swap n-1 items!

Is this faster than 

For a = 1 To numtiles
    swop = Int(Rnd * numtiles) + 1
    Swap tiles(a), tiles(swop)
Next
To me, it looks the same, only in reverse order    Huh
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Reply


Messages In This Thread
RE: Fisher Yates Shuffle for cards or any number of items - by PhilOfPerth - 06-20-2023, 02:06 AM



Users browsing this thread: 5 Guest(s)