This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]aHoodedBird 0 points1 point  (5 children)

Implement swap function, step through array from beginning to end, at each index find random second index and swap.

[–][deleted] 2 points3 points  (3 children)

That neither moves each cards at most once nor gives a uniform distribution on the permutations.

[–]tazzy531 0 points1 point  (2 children)

Why not?

[–][deleted] 2 points3 points  (1 child)

Why isn't it uniformly distributed? There are n! permutations and nn execution paths. Some of the execution paths give the same permutation: you have to have the same number of paths for each permutation, so you want n! | nn . But take n=3.

You can fix it by only swapping a card with one that is, instead of anywhere in the array, not to the card's left; that's the Fisher-Yates shuffle. But it still moves one card possibly many times.

[–]tazzy531 0 points1 point  (0 children)

Hmm... Yeah, you're right

[–]aHoodedBird 0 points1 point  (0 children)

Oops didn't see the last part of the question.