The Queen of my Heart

Given below are two algorithms to shuffle a deck of cards:

1
2
3
4
5
6
def aShuffle(l):
    cards = l[:]
    for i in xrange((len(cards) - 1), 0, -1):
            n = random.randrange(i+1)
            cards[i], cards[n] = cards[n], cards[i]
    return cards

1
2
3
4
5
6
def bShuffle(l):
    cards = l[:]
    for i in xrange(len(cards)):
            n = random.randrange(len(cards))
            cards[i], cards[n] = cards[n], cards[i]
    return cards

Which method's shuffles are closest to a random shuffling of cards?

Details

  • The more uniform the probability distribution between all possible permutations of the cards, the better the shuffling algorithm is.
  • Assume that random.randrange is perfectly uniformly random.
×

Problem Loading...

Note Loading...

Set Loading...