title: Debugging: Shuffle deck of cards in Python/ random
body: <p>I know the title sounds boring, because many people have already asked about this topic. I hope it can help me get some insight into how the random module works. The issue is, I wrote two different functions that I think should be identical, but the results I'm getting are not identical, and I don't understand why.</p>
  
  <p>I hope to end up with a "well-shuffled deck." I only care about whether cards are red or black, so my decks are very simple. I am calling "1" red and "0" black.</p>
  
  <p>My idea was to build the deck by appending a 1 (red) if random.random() is > .5, or a 0 (black) otherwise, and then just appending 1 or 0 automatically when I have reached 26 (half the deck) of one color. But something is going wrong. deckmaker() doesn't work properly, although deckmaker2() does. Can anyone provide insight?</p>
  
  <pre><code>import random
  
  def deckmaker():
      deck = []
      for i in range(52):
          if deck.count(0) == 26:
              deck.append(1)
          elif deck.count(1) == 26:
              deck.append(0)
          elif random.random() &gt; .5:
              deck.append(0)
          else:
              deck.append(1)
      return deck
  
  def deckmaker2():
      newdeck = []
      for i in range(26):
          newdeck.append(0)
      for i in range(26):
          newdeck.append(1)
      deck = []
      for i in range(52):
          x = random.randint(0,len(newdeck)-1)
          deck.append(newdeck.pop(x))
      return deck
  </code></pre>
  
  <p>Note: While writing this question I discovered the random.shuffle list operator, which does the same thing as my second function, so of course getting the shuffled deck turns out to be easy. But I still wonder why my original code doesn't do the same thing.</p>
  
  <p><strong>Edited:</strong> Sorry for being vague about the exact problem with deckmaker(). The thing is, I don't exactly understand what's wrong. It has to do with the fact that on the decks it produces, as you "turn over" the cards one by one, there are strategies that let you predict whether the "next card" is going to be red or black that don't work with decks created using random.shuffle</p>
  
  <p><strong>Edit 2:</strong> [lots more information] I will explain how I determined that deckmaker doesn't work, in case that is important.
  I was writing this program to model the puzzle posted here: <a href="http://www.thebigquestions.com/2013/12/17/tuesday-puzzle-4/" rel="nofollow">http://www.thebigquestions.com/2013/12/17/tuesday-puzzle-4/</a></p>
  
  <p>My strategy was going to be to remember the last few cards dealt, and use that information to determine when to decide to take the next card. I thought maybe after getting 5 "black" cards in a row, it was a good time to predict "red." I implemented it like so:</p>
  
  <pre><code>mycards = []
  
  for j in range(1000):
      mydeck = deckmaker(52)
      mem_length = 5
      mem = []
      for c in range(mem_length):
          mem.append(4)
      for i in range(len(mydeck)):
          if mem.count(0) == mem_length:
              mycards.append(mydeck[i])
              break
          elif i == len(mydeck)-1:
              mycards.append(mydeck[i])
              break
          else:
              mem.append(mydeck[i])
              mem.pop(0)
  
  
  x = float(mycards.count(1))
  
  print x/len(mycards)
  </code></pre>
  
  <p>The result more than half the cards I was taking (putting into the list mycards) were "red," a result I achieved by taking the card after 5 <em>red</em> cards in a row were drawn. This made no sense, so I looked for a different way to create the decks and got a more normal result. But I still don't know what was wrong with my original decks.</p>