<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Daniel VeazeyDaniel Veazey</title>
	
	<link>http://www.danielveazey.com</link>
	<description>Things I find interesting</description>
	<lastBuildDate>Thu, 02 May 2013 02:46:56 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/danielveazey" /><feedburner:info uri="danielveazey" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId>danielveazey</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><item>
		<title>Easter picnic</title>
		<link>http://feedproxy.google.com/~r/danielveazey/~3/jGzBHRs980Q/</link>
		<comments>http://www.danielveazey.com/photography/easter-picnic/#comments</comments>
		<pubDate>Sun, 08 Apr 2012 22:54:24 +0000</pubDate>
		<dc:creator>Daniel Veazey</dc:creator>
				<category><![CDATA[Photography]]></category>

		<guid isPermaLink="false">http://www.danielveazey.com/?p=721</guid>
		<description />
				<content:encoded><![CDATA[<p><strong>
<div class="ngg-galleryoverview" id="ngg-gallery-12-721">


	
	<!-- Thumbnails -->
		
	<div id="ngg-image-132" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.danielveazey.com/wp-content/gallery/easter-picnic/dsc_9462.jpg" title=" " class="thickbox" rel="set_12" >
								<img title="dsc_9462" alt="dsc_9462" src="http://www.danielveazey.com/wp-content/gallery/easter-picnic/thumbs/thumbs_dsc_9462.jpg" width="100" height="75" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-133" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.danielveazey.com/wp-content/gallery/easter-picnic/dsc_9468.jpg" title=" " class="thickbox" rel="set_12" >
								<img title="dsc_9468" alt="dsc_9468" src="http://www.danielveazey.com/wp-content/gallery/easter-picnic/thumbs/thumbs_dsc_9468.jpg" width="100" height="75" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-134" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.danielveazey.com/wp-content/gallery/easter-picnic/dsc_9473.jpg" title=" " class="thickbox" rel="set_12" >
								<img title="dsc_9473" alt="dsc_9473" src="http://www.danielveazey.com/wp-content/gallery/easter-picnic/thumbs/thumbs_dsc_9473.jpg" width="100" height="75" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-135" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.danielveazey.com/wp-content/gallery/easter-picnic/dsc_9475.jpg" title=" " class="thickbox" rel="set_12" >
								<img title="dsc_9475" alt="dsc_9475" src="http://www.danielveazey.com/wp-content/gallery/easter-picnic/thumbs/thumbs_dsc_9475.jpg" width="100" height="75" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-136" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.danielveazey.com/wp-content/gallery/easter-picnic/dsc_9481.jpg" title=" " class="thickbox" rel="set_12" >
								<img title="dsc_9481" alt="dsc_9481" src="http://www.danielveazey.com/wp-content/gallery/easter-picnic/thumbs/thumbs_dsc_9481.jpg" width="100" height="75" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-137" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.danielveazey.com/wp-content/gallery/easter-picnic/dsc_9483.jpg" title=" " class="thickbox" rel="set_12" >
								<img title="dsc_9483" alt="dsc_9483" src="http://www.danielveazey.com/wp-content/gallery/easter-picnic/thumbs/thumbs_dsc_9483.jpg" width="100" height="75" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-138" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.danielveazey.com/wp-content/gallery/easter-picnic/dsc_9484.jpg" title=" " class="thickbox" rel="set_12" >
								<img title="dsc_9484" alt="dsc_9484" src="http://www.danielveazey.com/wp-content/gallery/easter-picnic/thumbs/thumbs_dsc_9484.jpg" width="100" height="75" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-139" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.danielveazey.com/wp-content/gallery/easter-picnic/dsc_9485.jpg" title=" " class="thickbox" rel="set_12" >
								<img title="dsc_9485" alt="dsc_9485" src="http://www.danielveazey.com/wp-content/gallery/easter-picnic/thumbs/thumbs_dsc_9485.jpg" width="100" height="75" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-140" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.danielveazey.com/wp-content/gallery/easter-picnic/dsc_9489.jpg" title=" " class="thickbox" rel="set_12" >
								<img title="dsc_9489" alt="dsc_9489" src="http://www.danielveazey.com/wp-content/gallery/easter-picnic/thumbs/thumbs_dsc_9489.jpg" width="100" height="75" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-141" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.danielveazey.com/wp-content/gallery/easter-picnic/dsc_9490.jpg" title=" " class="thickbox" rel="set_12" >
								<img title="dsc_9490" alt="dsc_9490" src="http://www.danielveazey.com/wp-content/gallery/easter-picnic/thumbs/thumbs_dsc_9490.jpg" width="100" height="75" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-142" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.danielveazey.com/wp-content/gallery/easter-picnic/dsc_9494.jpg" title=" " class="thickbox" rel="set_12" >
								<img title="dsc_9494" alt="dsc_9494" src="http://www.danielveazey.com/wp-content/gallery/easter-picnic/thumbs/thumbs_dsc_9494.jpg" width="100" height="75" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-143" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.danielveazey.com/wp-content/gallery/easter-picnic/dsc_9500.jpg" title=" " class="thickbox" rel="set_12" >
								<img title="dsc_9500" alt="dsc_9500" src="http://www.danielveazey.com/wp-content/gallery/easter-picnic/thumbs/thumbs_dsc_9500.jpg" width="100" height="75" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-144" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.danielveazey.com/wp-content/gallery/easter-picnic/dsc_9503.jpg" title=" " class="thickbox" rel="set_12" >
								<img title="dsc_9503" alt="dsc_9503" src="http://www.danielveazey.com/wp-content/gallery/easter-picnic/thumbs/thumbs_dsc_9503.jpg" width="100" height="75" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-145" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.danielveazey.com/wp-content/gallery/easter-picnic/dsc_9505.jpg" title=" " class="thickbox" rel="set_12" >
								<img title="dsc_9505" alt="dsc_9505" src="http://www.danielveazey.com/wp-content/gallery/easter-picnic/thumbs/thumbs_dsc_9505.jpg" width="100" height="75" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-146" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://www.danielveazey.com/wp-content/gallery/easter-picnic/dsc_9506.jpg" title=" " class="thickbox" rel="set_12" >
								<img title="dsc_9506" alt="dsc_9506" src="http://www.danielveazey.com/wp-content/gallery/easter-picnic/thumbs/thumbs_dsc_9506.jpg" width="100" height="75" />
							</a>
		</div>
	</div>
	
		
 	 	
	<!-- Pagination -->
 	<div class='ngg-clear'></div>
 	
</div>

</strong></p>
<img src="http://feeds.feedburner.com/~r/danielveazey/~4/jGzBHRs980Q" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.danielveazey.com/photography/easter-picnic/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.danielveazey.com/photography/easter-picnic/</feedburner:origLink></item>
		<item>
		<title>Ubuntu moon over water</title>
		<link>http://feedproxy.google.com/~r/danielveazey/~3/b4HMCfzMU7w/</link>
		<comments>http://www.danielveazey.com/gimp/ubuntu-moon-over-water/#comments</comments>
		<pubDate>Fri, 30 Mar 2012 04:35:25 +0000</pubDate>
		<dc:creator>Daniel Veazey</dc:creator>
				<category><![CDATA[Gimp]]></category>
		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://www.danielveazey.com/?p=719</guid>
		<description><![CDATA[I made a wallpaper for my Ubuntu 12.04 installation today.]]></description>
				<content:encoded><![CDATA[<p>I made a wallpaper for my Ubuntu 12.04 installation today.</p>
<p><a href="http://www.danielveazey.com/wp-content/uploads/2012/03/ubuntu-logo-water-1920x1080.png"><img class="aligncenter size-large wp-image-720" title="Ubuntu moon over water" src="http://www.danielveazey.com/wp-content/uploads/2012/03/ubuntu-logo-water-1920x1080-1024x576.png" alt="Ubuntu moon reflected over water" width="620" height="348" /></a></p>
<img src="http://feeds.feedburner.com/~r/danielveazey/~4/b4HMCfzMU7w" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.danielveazey.com/gimp/ubuntu-moon-over-water/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.danielveazey.com/gimp/ubuntu-moon-over-water/</feedburner:origLink></item>
		<item>
		<title>Pybrain</title>
		<link>http://feedproxy.google.com/~r/danielveazey/~3/i2T1BFDKMqY/</link>
		<comments>http://www.danielveazey.com/python/pybrain/#comments</comments>
		<pubDate>Sun, 25 Mar 2012 05:53:01 +0000</pubDate>
		<dc:creator>Daniel Veazey</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[card games]]></category>
		<category><![CDATA[go fish]]></category>
		<category><![CDATA[pybrain]]></category>

		<guid isPermaLink="false">http://www.danielveazey.com/?p=710</guid>
		<description><![CDATA[Note to self: Learn more about Pybrain and how you can use it to make a computer opponent for your Go Fish game.]]></description>
				<content:encoded><![CDATA[<p>Note to self: Learn more about Pybrain and how you can use it to make a computer opponent for your Go Fish game.</p>
<img src="http://feeds.feedburner.com/~r/danielveazey/~4/i2T1BFDKMqY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.danielveazey.com/python/pybrain/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.danielveazey.com/python/pybrain/</feedburner:origLink></item>
		<item>
		<title>Thinking about Go Fish with Python</title>
		<link>http://feedproxy.google.com/~r/danielveazey/~3/rf_qXXooOwQ/</link>
		<comments>http://www.danielveazey.com/python/thinking-about-go-fish-with-python/#comments</comments>
		<pubDate>Tue, 20 Mar 2012 14:10:39 +0000</pubDate>
		<dc:creator>Daniel Veazey</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[card games]]></category>
		<category><![CDATA[go fish]]></category>

		<guid isPermaLink="false">http://www.danielveazey.com/?p=708</guid>
		<description><![CDATA[Today I&#8217;m thinking about the Go Fish program I want to write in Python. Writing code that follows the rules of the game seems fairly straightforward. But there are a few advances I&#8217;d like to make with this program from my last program that made the computer play War against itself. First, I want to [...]]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.danielveazey.com/wp-content/uploads/2012/03/fish.jpg"><img class="alignleft size-medium wp-image-709" title="fish" src="http://www.danielveazey.com/wp-content/uploads/2012/03/fish-300x225.jpg" alt="Fish" width="300" height="225" /></a>Today I&#8217;m thinking about the Go Fish program I want to write in Python. Writing code that follows the rules of the game seems fairly straightforward. But there are a few advances I&#8217;d like to make with this program from my last program that made the computer play <a title="Introduction to writing card games in Python" href="http://www.danielveazey.com/python/introduction-to-writing-card-games-in-python/">War against itself</a>.</p>
<p>First, I want to make it where a human player is playing against the computer. Sure, watching a computer play against itself is neat for a few seconds, but it gets boring if there is no interaction. Achieving this shouldn&#8217;t be too hard. I should be able to make it print a menu of all the cards in your hand, then you just choose the corresponding menu item for the card you want to ask your opponent for.</p>
<p>Second, I will have to make the computer adhere to a strategy. Instead of just playing the top card from its hand, like it does in War, the computer also will have to choose what to ask for from you. Maybe I could write it so it just goes through its list of cards and asks for each consecutive card in its hand. I also want to make it so if the computer draws something you have recently asked for, it will know to ask you for that card. Perhaps I could create a difficulty setting so the harder the opponent, the longer it remembers what cards you&#8217;ve asked for.</p>
<p>It seems kind of daunting, but I know that all I have to do is break it down into smaller pieces, and only work on them one at a time.</p>
<img src="http://feeds.feedburner.com/~r/danielveazey/~4/rf_qXXooOwQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.danielveazey.com/python/thinking-about-go-fish-with-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.danielveazey.com/python/thinking-about-go-fish-with-python/</feedburner:origLink></item>
		<item>
		<title>Introduction to writing card games in Python</title>
		<link>http://feedproxy.google.com/~r/danielveazey/~3/09Jr-YKVLyw/</link>
		<comments>http://www.danielveazey.com/python/introduction-to-writing-card-games-in-python/#comments</comments>
		<pubDate>Mon, 19 Mar 2012 16:26:52 +0000</pubDate>
		<dc:creator>Daniel Veazey</dc:creator>
				<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.danielveazey.com/?p=705</guid>
		<description><![CDATA[I want to make card games with Python, so I think about how to simulate a deck of cards. Some of the properties of a deck are such: It has 52 unique cards. Each card consists of a rank: 2 through 10 and Jack, Queen, King, Ace; and a suit: Clubs, Diamonds, Hearts or Spades. [...]]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.danielveazey.com/wp-content/uploads/2012/03/queen-of-hearts.jpg"><img class="alignright size-full wp-image-706" title="Queen of Hearts" src="http://www.danielveazey.com/wp-content/uploads/2012/03/queen-of-hearts.jpg" alt="Queen of Hearts" width="250" height="346" /></a>I want to make card games with Python, so I think about how to simulate a deck of cards. Some of the properties of a deck are such: It has 52 unique cards. Each card consists of a rank: 2 through 10 and Jack, Queen, King, Ace; and a suit: Clubs, Diamonds, Hearts or Spades. Cards move around from the deck to a player&#8217;s hand, to being &#8220;in play&#8221; on the table, or to the discard pile, or perhaps any number of places, depending on the rules of the game you are playing with them. For the sake of sanity, a card cannot be in more than one place at one time.</p>
<p>So creating a shuffled deck of cards is very simple. If you think of the values of Jack, Queen, King and Ace as 11, 12, 13, and 14, you can implement a deck with just a few lines of code in a function:</p>
<div id="wpshdo_1" class="wp-synhighlighter-outer"><div id="wpshdt_1" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_1"></a><a id="wpshat_1" class="wp-synhighlighter-title" href="#codesyntax_1"  onClick="javascript:wpsh_toggleBlock(1)" title="Click to show/hide code block">A deck of cards</a></td><td align="right"><a href="#codesyntax_1" onClick="javascript:wpsh_code(1)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.danielveazey.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_1" onClick="javascript:wpsh_print(1)" title="Print code"><img border="0" style="border: 0 none" src="http://www.danielveazey.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.danielveazey.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.danielveazey.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_1" class="wp-synhighlighter-inner" style="display: block;"><pre class="python" style="font-family:monospace;"><span class="kw1">import</span> <span class="kw3">random</span>
&nbsp;
<span class="kw1">def</span> shuffled_deck<span class="br0">&#40;</span><span class="br0">&#41;</span>:
    deck = <span class="br0">&#91;</span><span class="br0">&#93;</span>
    <span class="kw1">for</span> suit <span class="kw1">in</span> <span class="br0">&#91;</span><span class="st0">'Clubs'</span>, <span class="st0">'Diamonds'</span>, <span class="st0">'Hearts'</span>, <span class="st0">'Spades'</span><span class="br0">&#93;</span>:
        <span class="kw1">for</span> num <span class="kw1">in</span> <span class="kw2">xrange</span><span class="br0">&#40;</span>2, 15<span class="br0">&#41;</span>:
            deck.<span class="me1">append</span><span class="br0">&#40;</span><span class="br0">&#91;</span>num, suit<span class="br0">&#93;</span><span class="br0">&#41;</span>
    <span class="kw3">random</span>.<span class="me1">shuffle</span><span class="br0">&#40;</span>deck<span class="br0">&#41;</span>
    <span class="kw1">return</span> deck</pre></div></div>
<p>Now you can do all kinds of operations on this list of cards you&#8217;ve created, and that&#8217;s where you start getting into the rules of whatever game you&#8217;re playing. I decided to start out with the simplest game I could think of: War. But before I get into that, I want to make the cards a little more user friendly. Right now the cards have a number and a suit. But say you get the 12 of Clubs. That&#8217;s not really a card. It should be called the Queen of Clubs. I wrote another little function that will return the proper name for a card when passed the value of its rank:</p>
<div id="wpshdo_2" class="wp-synhighlighter-outer"><div id="wpshdt_2" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_2"></a><a id="wpshat_2" class="wp-synhighlighter-title" href="#codesyntax_2"  onClick="javascript:wpsh_toggleBlock(2)" title="Click to show/hide code block">Proper card names</a></td><td align="right"><a href="#codesyntax_2" onClick="javascript:wpsh_code(2)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.danielveazey.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_2" onClick="javascript:wpsh_print(2)" title="Print code"><img border="0" style="border: 0 none" src="http://www.danielveazey.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.danielveazey.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.danielveazey.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_2" class="wp-synhighlighter-inner" style="display: block;"><pre class="python" style="font-family:monospace;"><span class="kw1">def</span> card_name<span class="br0">&#40;</span>card<span class="br0">&#41;</span>:
    <span class="kw1">if</span> card == 11:
        <span class="kw1">return</span> <span class="st0">'Jack'</span>
    <span class="kw1">elif</span> card == 12:
        <span class="kw1">return</span> <span class="st0">'Queen'</span>
    <span class="kw1">elif</span> card == 13:
        <span class="kw1">return</span> <span class="st0">'King'</span>
    <span class="kw1">elif</span> card == 14:
        <span class="kw1">return</span> <span class="st0">'Ace'</span>
    <span class="kw1">else</span>:
        <span class="kw1">return</span> <span class="kw2">str</span><span class="br0">&#40;</span>card<span class="br0">&#41;</span></pre></div></div>
<p>Now I want to make the computer play War against itself. You might play War differently than this, but here are the rules I made it play by:</p>
<ol>
<li>The deck is shuffled and each player is dealt a hand of 26 cards.</li>
<li>Each player plays the first card from their hand. Whichever card has a higher rank wins, and the player takes both cards and places them in his or her &#8220;won&#8221; pile.</li>
<li>If the two cards in play are of the same rank, those remain on the table and each player reveals another card. Once a player wins a round, the tying cards from the previous round (or rounds) are all won by the winner of the most recent round.</li>
<li>When all cards are depleted from a player&#8217;s hand, the cards in that player&#8217;s &#8220;won&#8221; pile are shuffled and then become the player&#8217;s new hand, and play continues.</li>
<li>The game is won when one player has taken all the cards. If the final round is a tie, no one wins that round and the winner of the game is determined by who has the most cards.</li>
</ol>
<p>So with that, here is the full code for War:</p>
<div id="wpshdo_3" class="wp-synhighlighter-outer"><div id="wpshdt_3" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_3"></a><a id="wpshat_3" class="wp-synhighlighter-title" href="#codesyntax_3"  onClick="javascript:wpsh_toggleBlock(3)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_3" onClick="javascript:wpsh_code(3)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.danielveazey.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_3" onClick="javascript:wpsh_print(3)" title="Print code"><img border="0" style="border: 0 none" src="http://www.danielveazey.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.danielveazey.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.danielveazey.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_3" class="wp-synhighlighter-inner" style="display: block;"><pre class="python" style="font-family:monospace;"><span class="kw1">import</span> <span class="kw3">random</span>
&nbsp;
<span class="kw1">def</span> shuffled_deck<span class="br0">&#40;</span><span class="br0">&#41;</span>:
    deck = <span class="br0">&#91;</span><span class="br0">&#93;</span>
    <span class="kw1">for</span> suit <span class="kw1">in</span> <span class="br0">&#91;</span><span class="st0">'Clubs'</span>, <span class="st0">'Diamonds'</span>, <span class="st0">'Hearts'</span>, <span class="st0">'Spades'</span><span class="br0">&#93;</span>:
        <span class="kw1">for</span> num <span class="kw1">in</span> <span class="kw2">xrange</span><span class="br0">&#40;</span>2, 15<span class="br0">&#41;</span>:
            deck.<span class="me1">append</span><span class="br0">&#40;</span><span class="br0">&#91;</span>num, suit<span class="br0">&#93;</span><span class="br0">&#41;</span>
    <span class="kw3">random</span>.<span class="me1">shuffle</span><span class="br0">&#40;</span>deck<span class="br0">&#41;</span>
    <span class="kw1">return</span> deck
&nbsp;
<span class="kw1">def</span> card_name<span class="br0">&#40;</span>card<span class="br0">&#41;</span>:
    <span class="kw1">if</span> card == 11:
        <span class="kw1">return</span> <span class="st0">'Jack'</span>
    <span class="kw1">elif</span> card == 12:
        <span class="kw1">return</span> <span class="st0">'Queen'</span>
    <span class="kw1">elif</span> card == 13:
        <span class="kw1">return</span> <span class="st0">'King'</span>
    <span class="kw1">elif</span> card == 14:
        <span class="kw1">return</span> <span class="st0">'Ace'</span>
    <span class="kw1">else</span>:
        <span class="kw1">return</span> <span class="kw2">str</span><span class="br0">&#40;</span>card<span class="br0">&#41;</span>
&nbsp;
<span class="kw1">def</span> deal_cards<span class="br0">&#40;</span>deck<span class="br0">&#41;</span>:
    <span class="kw1">return</span> deck<span class="br0">&#91;</span>::2<span class="br0">&#93;</span>, deck<span class="br0">&#91;</span>1::2<span class="br0">&#93;</span>
&nbsp;
<span class="kw1">def</span> check_cards<span class="br0">&#40;</span>player1, player2<span class="br0">&#41;</span>:
    <span class="kw1">if</span> player1 <span class="sy0">&gt;</span> player2:
        <span class="kw1">return</span> <span class="st0">'player1'</span>
    <span class="kw1">elif</span> player1 <span class="sy0">&lt;</span> player2:
        <span class="kw1">return</span> <span class="st0">'player2'</span>
    <span class="kw1">else</span>:
        <span class="kw1">return</span> <span class="st0">'tied'</span>
&nbsp;
<span class="kw1">def</span> replenish<span class="br0">&#40;</span>wonpile<span class="br0">&#41;</span>:
    <span class="kw3">random</span>.<span class="me1">shuffle</span><span class="br0">&#40;</span>wonpile<span class="br0">&#41;</span>
    <span class="kw1">return</span> wonpile
&nbsp;
deck = shuffled_deck<span class="br0">&#40;</span><span class="br0">&#41;</span>
hand1, hand2 = deal_cards<span class="br0">&#40;</span>deck<span class="br0">&#41;</span>
ontable = <span class="br0">&#91;</span><span class="br0">&#93;</span>
<span class="kw2">round</span> = 0
hand1won = <span class="br0">&#91;</span><span class="br0">&#93;</span>
hand2won = <span class="br0">&#91;</span><span class="br0">&#93;</span>
&nbsp;
<span class="kw1">while</span> <span class="kw2">len</span><span class="br0">&#40;</span>hand1<span class="br0">&#41;</span> <span class="sy0">&gt;</span> 0 <span class="kw1">and</span> <span class="kw2">len</span><span class="br0">&#40;</span>hand2<span class="br0">&#41;</span> <span class="sy0">&gt;</span> 0:
    <span class="kw2">round</span> += 1
    <span class="kw1">print</span> <span class="st0">&quot;Round %d&quot;</span> <span class="sy0">%</span> <span class="kw2">round</span>
    card1 = hand1.<span class="me1">pop</span><span class="br0">&#40;</span>0<span class="br0">&#41;</span>
    card2 = hand2.<span class="me1">pop</span><span class="br0">&#40;</span>0<span class="br0">&#41;</span>
    ontable.<span class="me1">extend</span><span class="br0">&#40;</span><span class="br0">&#91;</span>card1, card2<span class="br0">&#93;</span><span class="br0">&#41;</span>
    <span class="kw1">print</span> <span class="st0">&quot;Player One plays the %s of %s.&quot;</span> <span class="sy0">%</span> <span class="br0">&#40;</span>card_name<span class="br0">&#40;</span>card1<span class="br0">&#91;</span>0<span class="br0">&#93;</span><span class="br0">&#41;</span>, card1<span class="br0">&#91;</span>1<span class="br0">&#93;</span><span class="br0">&#41;</span>
    <span class="kw1">print</span> <span class="st0">&quot;Player Two plays the %s of %s.&quot;</span> <span class="sy0">%</span> <span class="br0">&#40;</span>card_name<span class="br0">&#40;</span>card2<span class="br0">&#91;</span>0<span class="br0">&#93;</span><span class="br0">&#41;</span>, card2<span class="br0">&#91;</span>1<span class="br0">&#93;</span><span class="br0">&#41;</span>
    play_winner = check_cards<span class="br0">&#40;</span>card1<span class="br0">&#91;</span>0<span class="br0">&#93;</span>, card2<span class="br0">&#91;</span>0<span class="br0">&#93;</span><span class="br0">&#41;</span>
    <span class="kw1">if</span> play_winner == <span class="st0">'player1'</span>:
        <span class="kw1">print</span> <span class="st0">&quot;Player One wins the round.&quot;</span>
        hand1won.<span class="me1">extend</span><span class="br0">&#40;</span>ontable<span class="br0">&#41;</span>
        ontable = <span class="br0">&#91;</span><span class="br0">&#93;</span>
    <span class="kw1">elif</span> play_winner == <span class="st0">'player2'</span>:
        <span class="kw1">print</span> <span class="st0">&quot;Player Two wins the round.&quot;</span>
        hand2won.<span class="me1">extend</span><span class="br0">&#40;</span>ontable<span class="br0">&#41;</span>
        ontable = <span class="br0">&#91;</span><span class="br0">&#93;</span>
    <span class="kw1">elif</span> play_winner == <span class="st0">'tied'</span>:
        <span class="kw1">print</span> <span class="st0">&quot;The players tie for this round and the cards remain on the table.&quot;</span>
    <span class="kw1">print</span> <span class="st0">&quot;The score is now Player One: %s, Player Two: %s&quot;</span> <span class="sy0">%</span> <span class="br0">&#40;</span><span class="kw2">len</span><span class="br0">&#40;</span>hand1won<span class="br0">&#41;</span>+<span class="kw2">len</span><span class="br0">&#40;</span>hand1<span class="br0">&#41;</span>, <span class="kw2">len</span><span class="br0">&#40;</span>hand2won<span class="br0">&#41;</span>+<span class="kw2">len</span><span class="br0">&#40;</span>hand2<span class="br0">&#41;</span><span class="br0">&#41;</span>
    <span class="kw1">if</span> <span class="kw2">len</span><span class="br0">&#40;</span>hand1<span class="br0">&#41;</span> == 0 <span class="kw1">and</span> <span class="kw2">len</span><span class="br0">&#40;</span>hand1won<span class="br0">&#41;</span> <span class="sy0">&gt;</span> 0:
        <span class="kw1">print</span> <span class="st0">&quot;Player One replenishes.&quot;</span>
        hand1 = replenish<span class="br0">&#40;</span>hand1won<span class="br0">&#41;</span>
        hand1won = <span class="br0">&#91;</span><span class="br0">&#93;</span>
    <span class="kw1">if</span> <span class="kw2">len</span><span class="br0">&#40;</span>hand2<span class="br0">&#41;</span> == 0 <span class="kw1">and</span> <span class="kw2">len</span><span class="br0">&#40;</span>hand2won<span class="br0">&#41;</span> <span class="sy0">&gt;</span> 0:
        <span class="kw1">print</span> <span class="st0">&quot;Player Two replenishes.&quot;</span>
        hand2 = replenish<span class="br0">&#40;</span>hand2won<span class="br0">&#41;</span>
        hand2won = <span class="br0">&#91;</span><span class="br0">&#93;</span>
&nbsp;
<span class="kw1">if</span> <span class="kw2">len</span><span class="br0">&#40;</span>hand1<span class="br0">&#41;</span> <span class="sy0">&gt;</span> <span class="kw2">len</span><span class="br0">&#40;</span>hand2<span class="br0">&#41;</span>:
    <span class="kw1">print</span> <span class="st0">&quot;Player One wins the game!&quot;</span>
<span class="kw1">elif</span> <span class="kw2">len</span><span class="br0">&#40;</span>hand1<span class="br0">&#41;</span> <span class="sy0">&lt;</span> <span class="kw2">len</span><span class="br0">&#40;</span>hand2<span class="br0">&#41;</span>:
    <span class="kw1">print</span> <span class="st0">&quot;Player Two wins the game!&quot;</span></pre></div></div>
<p>I think I will try a game of Go Fish next.</p>
<img src="http://feeds.feedburner.com/~r/danielveazey/~4/09Jr-YKVLyw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.danielveazey.com/python/introduction-to-writing-card-games-in-python/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.danielveazey.com/python/introduction-to-writing-card-games-in-python/</feedburner:origLink></item>
		<item>
		<title>A PyPy experiment</title>
		<link>http://feedproxy.google.com/~r/danielveazey/~3/ZpkDjTOX7Xw/</link>
		<comments>http://www.danielveazey.com/python/a-pypy-experiment/#comments</comments>
		<pubDate>Fri, 16 Mar 2012 14:52:51 +0000</pubDate>
		<dc:creator>Daniel Veazey</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[monty hall problem]]></category>
		<category><![CDATA[pypy]]></category>

		<guid isPermaLink="false">http://www.danielveazey.com/?p=704</guid>
		<description><![CDATA[I really have no idea what PyPy is. All I know is that it is an implementation of Python written in Python (?) and that it runs faster than Python. For example, I tested the Monty Hall Problem that I wrote recently, and timed them. Here is the result: [daniel@TimeMachine Python]$ time python2 monty_hall_switch.py All [...]]]></description>
				<content:encoded><![CDATA[<p>I really have no idea what PyPy is. All I know is that it is an implementation of Python written in Python (?) and that it runs faster than Python. For example, I tested the <a title="The Monty Hall Problem" href="http://www.danielveazey.com/python/the-monty-hall-problem/">Monty Hall Problem</a> that I wrote recently, and timed them. Here is the result:</p>
<pre>[daniel@TimeMachine Python]$ time python2 monty_hall_switch.py
All choices were switched.
Wins: 66679
Losses: 33321
real 0m1.265s
user 0m1.243s
sys 0m0.023s
[daniel@TimeMachine Python]$ time pypy monty_hall_switch.py
All choices were switched.
Wins: 66853
Losses: 33147
real 0m0.594s
user 0m0.477s
sys 0m0.070s</pre>
<p>So PyPy ran the program more than twice as fast as Python. Pretty neat, huh?</p>
<img src="http://feeds.feedburner.com/~r/danielveazey/~4/ZpkDjTOX7Xw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.danielveazey.com/python/a-pypy-experiment/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.danielveazey.com/python/a-pypy-experiment/</feedburner:origLink></item>
		<item>
		<title>pyvideo.org – Keynote: David Beazley on PyPy</title>
		<link>http://feedproxy.google.com/~r/danielveazey/~3/rsvipNGFRuY/</link>
		<comments>http://www.danielveazey.com/python/pyvideo-org-keynote-david-beazley-on-pypy/#comments</comments>
		<pubDate>Fri, 16 Mar 2012 13:47:56 +0000</pubDate>
		<dc:creator>Daniel Veazey</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[pypy]]></category>

		<guid isPermaLink="false">http://www.danielveazey.com/?p=703</guid>
		<description><![CDATA[pyvideo.org &#8211; Keynote: David Beazley.]]></description>
				<content:encoded><![CDATA[<p><a href="http://pyvideo.org/video/659/keynote-david-beazley">pyvideo.org &#8211; Keynote: David Beazley</a>.</p>
<img src="http://feeds.feedburner.com/~r/danielveazey/~4/rsvipNGFRuY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.danielveazey.com/python/pyvideo-org-keynote-david-beazley-on-pypy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.danielveazey.com/python/pyvideo-org-keynote-david-beazley-on-pypy/</feedburner:origLink></item>
		<item>
		<title>A piano keyboard in Inkscape</title>
		<link>http://feedproxy.google.com/~r/danielveazey/~3/yFONZ8xZnz8/</link>
		<comments>http://www.danielveazey.com/inkscape/a-piano-keyboard-in-inkscape/#comments</comments>
		<pubDate>Thu, 15 Mar 2012 20:47:42 +0000</pubDate>
		<dc:creator>Daniel Veazey</dc:creator>
				<category><![CDATA[Inkscape]]></category>
		<category><![CDATA[keyboard]]></category>

		<guid isPermaLink="false">http://www.danielveazey.com/?p=701</guid>
		<description />
				<content:encoded><![CDATA[<p><a href="http://www.danielveazey.com/wp-content/uploads/2012/03/keyboard.png"><img class="alignright size-large wp-image-702" title="Piano keyboard" src="http://www.danielveazey.com/wp-content/uploads/2012/03/keyboard-1024x650.png" alt="Piano keyboard" width="584" height="370" /></a></p>
<img src="http://feeds.feedburner.com/~r/danielveazey/~4/yFONZ8xZnz8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.danielveazey.com/inkscape/a-piano-keyboard-in-inkscape/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.danielveazey.com/inkscape/a-piano-keyboard-in-inkscape/</feedburner:origLink></item>
		<item>
		<title>Self portrait</title>
		<link>http://feedproxy.google.com/~r/danielveazey/~3/ipIiA7fxSMo/</link>
		<comments>http://www.danielveazey.com/photography/self-portrait/#comments</comments>
		<pubDate>Wed, 14 Mar 2012 21:57:58 +0000</pubDate>
		<dc:creator>Daniel Veazey</dc:creator>
				<category><![CDATA[Photography]]></category>

		<guid isPermaLink="false">http://www.danielveazey.com/?p=699</guid>
		<description />
				<content:encoded><![CDATA[<p><a href="http://www.danielveazey.com/wp-content/uploads/2012/03/bwprofile.jpg"><img class="alignright size-medium wp-image-700" title="Daniel Veazey" src="http://www.danielveazey.com/wp-content/uploads/2012/03/bwprofile-300x300.jpg" alt="Daniel Veazey self portrait" width="300" height="300" /></a></p>
<img src="http://feeds.feedburner.com/~r/danielveazey/~4/ipIiA7fxSMo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.danielveazey.com/photography/self-portrait/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.danielveazey.com/photography/self-portrait/</feedburner:origLink></item>
		<item>
		<title>Calculating and testing probability with Python</title>
		<link>http://feedproxy.google.com/~r/danielveazey/~3/IEeoARMCmHc/</link>
		<comments>http://www.danielveazey.com/python/calculating-and-testing-probability-with-python/#comments</comments>
		<pubDate>Thu, 08 Mar 2012 22:49:59 +0000</pubDate>
		<dc:creator>Daniel Veazey</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[binomial test]]></category>
		<category><![CDATA[hitchhiker's guide to the galaxy]]></category>
		<category><![CDATA[probability]]></category>

		<guid isPermaLink="false">http://www.danielveazey.com/?p=696</guid>
		<description><![CDATA[&#8220;Please do not be alarmed by anything you see or hear around you … We are now cruising at a level of two to the power of two hundred and seventy-six thousand to one against and falling, and we will be restoring normality just as soon as we are sure what is normal anyway. Thank [...]]]></description>
				<content:encoded><![CDATA[<blockquote><p>&#8220;Please do not be alarmed by anything you see or hear around you … We are now cruising at a level of two to the power of two hundred and seventy-six thousand to one against and falling, and we will be restoring normality just as soon as we are sure what is normal anyway. Thank you …&#8221;</p></blockquote>
<p><a href="http://www.danielveazey.com/wp-content/uploads/2012/03/flipping_a_coin.jpg"><img class="alignright size-medium wp-image-697" title="Flipping a coin" src="http://www.danielveazey.com/wp-content/uploads/2012/03/flipping_a_coin-218x300.jpg" alt="Flipping a coin" width="218" height="300" /></a>Inside a bag are 9 coins. 4 of these coins are fair, in that they have a 50% chance of coming up heads. The other 5 coins in the bag are unfair coins. When they are flipped, they have a 65% chance of coming up heads. What are the chances, expressed as a percentage, that when drawing a random coin from the bag and flipping it 3 times, it will come up heads all 3 times?</p>
<p>Finding the answer is slightly tedious, but not difficult. First you have to consider what would happen if you randomly chose a fair coin from the bag, then you have to consider what would happen if you randomly chose an unfair coin. Add those two possibilities together, and there&#8217;s your answer.</p>
<h3>Fair coin</h3>
<p>First, let&#8217;s look at what happens when you draw a fair coin from the bag and flip it three times. The chances of one flip coming up heads are 50%. Twice would be 50% squared, and three times would be 50% cubed. You can express this as .5^3, or .125, or 12.5%. Since only 4 of the 9 coins in the bag are fair, you need to multiply .125 by 4/9. That puts the chances of pulling a fair coin and having it come up heads on all three flips at about 5.56%.</p>
<h3>Unfair coin</h3>
<p>The way to find the chances of pulling an unfair coin and getting three heads is the same. .65^3 = about 27.46%, and since there are 5 unfair coins from 9 total, .2746 * 5/9 = about .1526, or 15.26%.</p>
<h3>Add them up</h3>
<p>All that&#8217;s left now is to add the chances of the two possibilities together to get the total chances of pulling any coin from the bag and getting three heads. 5.56% + 15.26% = 20.82%. The equation should look like this:</p>
<pre>probability = (.50 ^ 3 * 4 / 9) + (.65 ^ 3 * 5 / 9)</pre>
<p>And when you do it that way, you end up with about 20.81% instead of 20.82%, because you&#8217;re only rounding the final answer instead of rounding the answers to intermediate steps.</p>
<h3>The proof</h3>
<p>So I wanted to test this out and see what Python would do when simulating such a scenario 100,000 times. Here is the code I came up with:</p>
<div id="wpshdo_4" class="wp-synhighlighter-outer"><div id="wpshdt_4" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_4"></a><a id="wpshat_4" class="wp-synhighlighter-title" href="#codesyntax_4"  onClick="javascript:wpsh_toggleBlock(4)" title="Click to show/hide code block">coinflips.py</a></td><td align="right"><a href="#codesyntax_4" onClick="javascript:wpsh_code(4)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.danielveazey.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_4" onClick="javascript:wpsh_print(4)" title="Print code"><img border="0" style="border: 0 none" src="http://www.danielveazey.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.danielveazey.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.danielveazey.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_4" class="wp-synhighlighter-inner" style="display: block;"><pre class="python" style="font-family:monospace;"><span class="co1">#  coinflips.py</span>
<span class="co1">#  a program to test the concept presented at</span>
<span class="co1">#  http://www.khanacademy.org/math/probability/v/dependent-probability-example-1</span>
<span class="co1">#  by Daniel Veazey &lt;danielveazey@gmail.com&gt;</span>
&nbsp;
<span class="kw1">import</span> <span class="kw3">random</span>
&nbsp;
<span class="co1">#  what happens when you flip a coin a number of times, depending on</span>
<span class="co1">#  whether the coin is fair or unfair (weighted)</span>
<span class="co1">#  a pseudo-random number is genereated from 1 to 100</span>
<span class="co1">#  and compared to what its odds of landing on heads are</span>
<span class="co1">#  return True for heads if result is below odds</span>
<span class="co1">#  or return False for tails if the result is above the odds</span>
<span class="kw1">def</span> flipping<span class="br0">&#40;</span>how_many_flips, odds<span class="br0">&#41;</span>:
    no_of_heads = 0
    <span class="kw1">for</span> flip <span class="kw1">in</span> <span class="kw2">range</span><span class="br0">&#40;</span>0, how_many_flips<span class="br0">&#41;</span>:
        flip_result = <span class="kw3">random</span>.<span class="me1">randint</span><span class="br0">&#40;</span>1, 100<span class="br0">&#41;</span>
        <span class="kw1">if</span> flip_result <span class="sy0">&lt;</span>= odds:
            no_of_heads += 1
    <span class="kw1">return</span> no_of_heads == how_many_flips
&nbsp;
<span class="co1">#  choosing a coin pseudo-randomly</span>
<span class="co1">#  pseudo-random integer is generated from the number of coins available</span>
<span class="co1">#  and compared to the number of fair coins available</span>
<span class="co1">#  True = unfair coin; False = fair coin</span>
<span class="kw1">def</span> choosing_coin<span class="br0">&#40;</span>how_many_fair, how_many_unfair<span class="br0">&#41;</span>:
    total_coins = how_many_fair + how_many_unfair
    chosen_coin = <span class="kw3">random</span>.<span class="me1">randint</span><span class="br0">&#40;</span>1, total_coins<span class="br0">&#41;</span>
    <span class="kw1">return</span> chosen_coin <span class="sy0">&gt;</span> how_many_fair
&nbsp;
<span class="co1">#  ask user for parameters of test</span>
<span class="kw1">print</span> <span class="st0">&quot;Testing probability of fair and unfair coin flips when a random coin is pulled from a bag.<span class="es0">\n</span>&quot;</span>
fair_coins = <span class="kw2">float</span><span class="br0">&#40;</span><span class="kw2">raw_input</span><span class="br0">&#40;</span><span class="st0">&quot;How many fair coins do you wish to put in the bag? &quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
unfair_coins = <span class="kw2">float</span><span class="br0">&#40;</span><span class="kw2">raw_input</span><span class="br0">&#40;</span><span class="st0">&quot;How many unfair coins do you wish to put in the bag? &quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
unfair_odds = <span class="kw2">float</span><span class="br0">&#40;</span><span class="kw2">raw_input</span><span class="br0">&#40;</span><span class="st0">&quot;For the unfair coins, what percent chance does one flip have that it will come up heads? &quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
number_of_flips = <span class="kw2">float</span><span class="br0">&#40;</span><span class="kw2">raw_input</span><span class="br0">&#40;</span><span class="st0">&quot;How many times do you want to flip the coin chosen from the bag? &quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
&nbsp;
<span class="co1">#  tell the user what the probability is</span>
<span class="kw1">print</span> <span class="st0">&quot;<span class="es0">\n</span>The chances of getting&quot;</span>, <span class="kw2">int</span><span class="br0">&#40;</span>number_of_flips<span class="br0">&#41;</span>, <span class="st0">&quot;heads in a row are about&quot;</span>, <span class="kw2">float</span><span class="br0">&#40;</span><span class="st0">&quot;%.4f&quot;</span> <span class="sy0">%</span> <span class="br0">&#40;</span><span class="br0">&#40;</span><span class="br0">&#40;</span><span class="br0">&#40;</span>unfair_odds / <span class="nu0">100.00</span><span class="br0">&#41;</span> <span class="sy0">**</span> number_of_flips<span class="br0">&#41;</span> <span class="sy0">*</span> unfair_coins / <span class="br0">&#40;</span>fair_coins + unfair_coins<span class="br0">&#41;</span><span class="br0">&#41;</span> + <span class="br0">&#40;</span><span class="br0">&#40;</span>.50 <span class="sy0">**</span> number_of_flips<span class="br0">&#41;</span> <span class="sy0">*</span> fair_coins / <span class="br0">&#40;</span>fair_coins + unfair_coins<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">*</span><span class="nu0">100.00</span>, <span class="st0">&quot;percent.&quot;</span>
<span class="kw1">print</span> <span class="st0">&quot;I am now running the simulation 100,000 times. This may take a few seconds.<span class="es0">\n</span>&quot;</span>
&nbsp;
<span class="co1">#  the simulation: success is the number of times the flips came up all heads</span>
success = 0
<span class="kw1">for</span> x <span class="kw1">in</span> <span class="kw2">range</span><span class="br0">&#40;</span><span class="nu0">100000</span><span class="br0">&#41;</span>:
    <span class="co1">#  choose a coin</span>
    <span class="kw1">if</span> choosing_coin<span class="br0">&#40;</span>fair_coins, unfair_coins<span class="br0">&#41;</span> == <span class="kw2">True</span>:
        <span class="co1">#  flipping an unfair coin</span>
        <span class="kw1">if</span> flipping<span class="br0">&#40;</span><span class="kw2">int</span><span class="br0">&#40;</span>number_of_flips<span class="br0">&#41;</span>, <span class="br0">&#40;</span>unfair_odds<span class="br0">&#41;</span><span class="br0">&#41;</span> == <span class="kw2">True</span>:
            success += <span class="nu0">1</span>
    <span class="co1">#  flipping a fair coin</span>
    <span class="kw1">else</span>:
        <span class="kw1">if</span> flipping<span class="br0">&#40;</span><span class="kw2">int</span><span class="br0">&#40;</span>number_of_flips<span class="br0">&#41;</span>, 50<span class="br0">&#41;</span> == <span class="kw2">True</span>:
            success += 1
<span class="kw1">print</span> <span class="st0">&quot;Simulation complete. All heads came up&quot;</span>, success, <span class="st0">&quot;out of 100,000 times.&quot;</span></pre></div></div>
<p>And here is what Python told me:</p>
<pre>Testing probability of fair and unfair coin flips when a random coin is pulled from a bag.
How many fair coins do you wish to put in the bag? 4
How many unfair coins do you wish to put in the bag? 5
For the unfair coins, what percent chance does one flip have that it will come up heads? 65
How many times do you want to flip the coin chosen from the bag? 3

The chances of getting 3 heads in a row are about 20.81 percent.
I am now running the simulation 100,000 times. This may take a few seconds.

Simulation complete. All heads came up 20621 out of 100,000 times.</pre>
<h3>How it works</h3>
<p>After importing the random module, the first function I defined is flipping(how_many_flips, odds). It generates a pseudo-random number and compares it to the odds that are passed to the function. If the number comes in under the odds, then it adds one to the variable no_of_heads. It does this again as many times as necessary to meet the value of how_many_flips were passed to it. Once it has done that, it returns True if no_of_heads is equal to how_many_flips. Otherwise it returns False.</p>
<p>The second function defined is choosing_coin(how_many_fair, how_many_unfair). Another pseudo-random number is generated, between 1 and the total number of coins that are available. If the random number is greater than how_many_fair, the function returns True to tell that an unfair coin was chosen. Otherwise it returns False.</p>
<p>Now Python asks for some data from the user. It wants to know how many fair_coins and unfair_coins there are, how unfair the unfair coins are (unfair_odds), and number_of_flips that the user wants it to do on the chosen coin.</p>
<p>All these variables are assigned as floating point variables, but there is room for improvement here. It would be better if I had made them decimals, but it seems that whenever I try to use decimals in Python, instead of getting what I&#8217;m expecting, I get stuff like this:</p>
<pre>&gt;&gt;&gt; decimal.getcontext().prec = 2
&gt;&gt;&gt; decimal.Decimal(4.56)
Decimal('4.55999999999999960920149533194489777088165283203125')
&gt;&gt;&gt; x = decimal.Decimal(4.56)
&gt;&gt;&gt; x
Decimal('4.55999999999999960920149533194489777088165283203125')
&gt;&gt;&gt; y = decimal.Decimal(2.3)
&gt;&gt;&gt; y
Decimal('2.29999999999999982236431605997495353221893310546875')
&gt;&gt;&gt; x + y
Decimal('6.9')
&gt;&gt;&gt;</pre>
<p>So I&#8217;m leaving it as floating point variables right now. I need to read more about decimals in Python, and once I figure out how to use them correctly, I might change those variables to decimals if I haven&#8217;t moved on to bigger and better things. Now back to the program.</p>
<p>After getting the values from the user, Python calculates the probability using the previously mentioned equation. Then it runs a simulation using those numbers 100,000 times. It calls the choosing_coin() function, and if it is returned True for an unfair coin, it calls the flipping() function and passes the unfair_odds to it. If choosing_coin() returns False, the program calls flipping() with 50 as the odds. When it passes number_of_flips to the flipping() function, it passes it as an integer because a for loop requires the values in the range to be integers, and it would encounter an error if it tried to use a floating point variable as one of the range&#8217;s values.</p>
<p>So if flipping() returns True for either using the unfair_odds or 50 for fair odds, Python keeps track of it by adding 1 to the value of the variable named success. Finally, it tells the results of its tests. It&#8217;s usually very close to its corresponding probability.</p>
<h3>Recommended reading</h3>
<p><a href="http://en.wikipedia.org/wiki/Binomial_test">http://en.wikipedia.org/wiki/Binomial_test</a></p>
<img src="http://feeds.feedburner.com/~r/danielveazey/~4/IEeoARMCmHc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.danielveazey.com/python/calculating-and-testing-probability-with-python/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.danielveazey.com/python/calculating-and-testing-probability-with-python/</feedburner:origLink></item>
	</channel>
</rss><!-- Dynamic page generated in 2.050 seconds. --><!-- Cached page generated by WP-Super-Cache on 2013-05-08 12:29:37 -->
