proc/TrueRandom(list/TheDeck)
var
size = TheDeck.len
while(size>0)
TheDeck.Swap(rand(1,size),rand(1,TheDeck.len))
size--
I've done alot of online reading lately about how to randomly shuffle a deck of 52 cards.
From what I read they all ultimately suggest to;
Switch A with B
Where A = A number between 1 and LoopCount
and B = Number btween 1 and DeckMax
LoopCount starts at the Deck's size and decreases.
This is proposed to be truly random as EACH position in the deck has an exactly equal chance of being swapped with another.
Lummox JR wrote:
This is a correct shuffling algorithm:
proc/Shuffle(list/deck)
> if(!deck) return
> for(var/i=deck.len, i>1, --i)
> deck.Swap(i, rand(1,i))
Essentially what that's doing is building the shuffled deck from the bottom up, choosing a card at random from the unshuffled part (indexes 1 through i) and moving it to the shuffled pile (i+1 through deck.len), replacing it with another card. As the loop progresses, the growing shuffled pile is filled with cards in truly random order while the unshiffled pile becomes more random as it gets smaller.
The loop terminates when i==1 because at that point, the process of elimination has left you with only one card.
I'm not sure if this method has equal possibilities for each permutation, however here's a quick way to do it: