<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	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/"
	>

<channel>
	<title>Web Cash &#187; random</title>
	<atom:link href="http://www.earn-web-cash.com/tag/random/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.earn-web-cash.com</link>
	<description>Writing, Designing, and Making Money Online</description>
	<lastBuildDate>Sun, 04 Dec 2011 22:52:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>How to Randomize AdSense Ads with Javascript</title>
		<link>http://www.earn-web-cash.com/2008/02/29/random-adsense-javascript/</link>
		<comments>http://www.earn-web-cash.com/2008/02/29/random-adsense-javascript/#comments</comments>
		<pubDate>Fri, 29 Feb 2008 23:45:30 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Advertising]]></category>
		<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[JS Tutorials]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[ad]]></category>
		<category><![CDATA[AdSense]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[random]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/29/random-adsense-javascript/</guid>
		<description><![CDATA[I came across this problem on the forums today. How can you randomize which Ad Sense ad to show using Javascript? The solution, it turns out, is very simple. Why Randomize Ads? There are a few reasons you might want to do this. If you&#8217;re looking into changing the theme of your AdSense ad, you [...]]]></description>
			<content:encoded><![CDATA[<p>I came across this problem on the forums today.  How can you randomize which Ad Sense ad to show using Javascript?</p>
<p>The solution, it turns out, is very simple.<br />
<span id="more-164"></span></p>
<h4>Why Randomize Ads?</h4>
<p>There are a few reasons you might want to do this.</p>
<p>If you&#8217;re looking into changing the theme of your AdSense ad, you may want to test the two themes against each other.  By giving each theme approximately 50% of the impressions for a given time period, you can make some direct comparisons between their performance.</p>
<p>Or maybe you and a friend both run a website, so you want to split the advertising time between two AdSense publisher ids.  That can be done too.</p>
<h4>How the AdSense Code Works</h4>
<p>Before we randomize it, let&#8217;s take a look at how the AdSense code works.</p>
<p>Here&#8217;s the snippet of code that creates the skyscraper on the left side of this page.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span><span style="color: #808080; font-style: italic;">&lt;!--</span>
<span style="color: #808080; font-style: italic;">google_ad_client = &quot;pub-2399151883698113&quot;;</span>
<span style="color: #808080; font-style: italic;">/* Web Cash Tall Skyscraper */</span>
<span style="color: #808080; font-style: italic;">google_ad_slot = &quot;8702750734&quot;;</span>
<span style="color: #808080; font-style: italic;">google_ad_width = 160;</span>
<span style="color: #808080; font-style: italic;">google_ad_height = 600;</span>
<span style="color: #808080; font-style: italic;">//--&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span></span>
<span style="color: #009900;"><span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></div></div>

<p>You&#8217;ll notice two major pieces to this code.  The first piece &#8211; in the first <code>&lt;script&gt;</code> element &#8211; defines the ad unit&#8217;s properties.  It sets google_ad_client (your publisher id), google_ad_slot (the saved ad unit&#8217;s id with color info), google_ad_width, and google_ad_height.</p>
<p>The second section &#8211; the script element with src &#8220;show_ads.js&#8221; &#8211; actually displays the ad based on the variables that were set previously.</p>
<p>In order to vary the type of ad shown, we need to come up with a conditional statement to vary the google_ad_client/slot/width/height settings.</p>
<h4>The Javascript Code&#8230;</h4>
<p>And here&#8217;s the moment you&#8217;ve all been waiting for &#8211; the Javascript code to randomize which ad is shown.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;!--</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0.5</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  google_ad_client <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;pub-2399151883698113&quot;</span><span style="color: #339933;">;</span>
  google_ad_slot <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;5396010225&quot;</span><span style="color: #339933;">;</span>
  google_ad_width <span style="color: #339933;">=</span> <span style="color: #CC0000;">250</span><span style="color: #339933;">;</span>
  google_ad_height <span style="color: #339933;">=</span> <span style="color: #CC0000;">250</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
  google_ad_client <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;pub-2399151883698113&quot;</span><span style="color: #339933;">;</span>
  google_ad_slot <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;3900536874&quot;</span><span style="color: #339933;">;</span>
  google_ad_width <span style="color: #339933;">=</span> <span style="color: #CC0000;">250</span><span style="color: #339933;">;</span>
  google_ad_height <span style="color: #339933;">=</span> <span style="color: #CC0000;">250</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #006600; font-style: italic;">//--&gt;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
&lt;script type=&quot;text/javascript&quot;
src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot;&gt;
&lt;/script&gt;</pre></div></div>

<p>You&#8217;ll notice that there&#8217;s an if/else statement, and each branch of the conditional statement defines a different ad to display.  The results of that conditional statement rely on the outcome of Math.random().</p>
<p>Previously, we built a <a href="http://www.earn-web-cash.com/2008/02/24/random-number-function">function to get a useful random number out of Math.random()</a>.  This is actually a situation where we can use the value from Math.random() just the way it is.</p>
<p>Math.random() generates a random value between 0 and 1.  In other words, it gives you a probability.  By checking that Math.random() is greater than 0.5, the statement should evaluate to true approximately 50% of the time.</p>
<p>We could also make the rotation more lop-sided.  For example (Math.random() > 0.70) would be true 30% of the time.  Therefore the first ad would be displayed 30% of the time, while the second ad would be displayed 70% of the time.</p>
<p>I hope you weren&#8217;t looking for something more complicated because, well, it isn&#8217;t.  It&#8217;s just that simple.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/29/random-adsense-javascript/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Combinations: General Function to Get All Possible Combinations</title>
		<link>http://www.earn-web-cash.com/2008/02/28/all-combinations-general-function/</link>
		<comments>http://www.earn-web-cash.com/2008/02/28/all-combinations-general-function/#comments</comments>
		<pubDate>Fri, 29 Feb 2008 00:15:54 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Game Design]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[combinations]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[probability]]></category>
		<category><![CDATA[random]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/28/all-combinations-general-function/</guid>
		<description><![CDATA[Previously, we looked at how to calculate a binomial coefficient (to find the total number of possible combinations) and an example of how to generate a list of combinations with hard-coded loops. A function would be much more useful to us if it was flexible &#8211; if it could generate a list of combinations for [...]]]></description>
			<content:encoded><![CDATA[<p>Previously, we looked at <a href="http://www.earn-web-cash.com/2008/02/24/combinations-binomial-coefficients/">how to calculate a binomial coefficient</a> (to find the total number of possible combinations) and an example of <a href="http://www.earn-web-cash.com/2008/02/26/get-all-combinations/">how to generate a list of combinations with hard-coded loops</a>.</p>
<p>A function would be much more useful to us if it was flexible &#8211; if it could generate a list of combinations for any size set of numbers.  We can&#8217;t do this with the hard coded loop solution, so we need to come up with a better method.<br />
<span id="more-163"></span></p>
<h4>The Function Concept</h4>
<p>Logically, the best way to do this is to create a function that takes a set of numbers (one combination), the size of the set being chosen from (x in [xCy]), and the size of the set being chosen (y in [xCy]).  The function would then use a sequential pattern to calculate the next possible combination and return it.</p>
<p>With this type of function, we could create a while loop that iterates through the whole set of possible combinations.  We initialize it with the first possible combination, continue to execute while the function returns a combination, and finish when the function returns 0 &#8211; in which case there are no more combinations.</p>
<p>If we&#8217;re finding the possible combinations for (11 Choose 4), the main loop would look something like this.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$combo</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$total</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">11</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$choose</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$combo</span> <span style="color: #339933;">=</span> getNextCombo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$combo</span><span style="color: #339933;">,</span> <span style="color: #000088;">$total</span><span style="color: #339933;">,</span> <span style="color: #000088;">$choose</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">//  Process the combo here</span>
  <span style="color: #666666; font-style: italic;">//  Store it in a file, an array, or something</span>
  <span style="color: #666666; font-style: italic;">//  Or just out put it</span>
  <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$combo</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$number</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$number</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' '</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This initializes an empty array &#8211; $combo.  Our function takes the array and populates it with the next possible combination.</p>
<p>If the array is empty, it&#8217;ll create the first combination &#8211; {1, 2, 3, 4}.  Otherwise, it will increment that set by one to get the next possible combination set &#8211; {1, 2, 3, 5}.  When the function receives a combination set it can&#8217;t possibly increment &#8211; {8, 9, 10, 11} &#8211; it returns false and the loop ends.</p>
<h4>Ok&#8230; How Do We Build the Function?</h4>
<p>Let&#8217;s start by creating the function, checking for an empty array, and returning the initial set.  Then we can dive into how we go about incrementing one combination set to get the next one.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> getNextCombo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$combo</span><span style="color: #339933;">,</span> <span style="color: #000088;">$total</span><span style="color: #339933;">,</span> <span style="color: #000088;">$choose</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$combo</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//  Return the first set, i.e. {1, 2, 3, 4}</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$choose</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$combo</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$combo</span><span style="color: #339933;">;</span>    
  <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//  Here we'll calculate the next combination</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//  If we didn't return before, no combination was found</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>That should be pretty self-explanatory.  Now we can look at our pattern and develop a method of calculating the next possible combination.</p>
<h4>The Pattern</h4>
<p>In the last article on <a href="http://www.earn-web-cash.com/2008/02/26/get-all-combinations/">getting combinations</a>, we identified the basic pattern to follow.</p>
<ul>
<li>Find the &#8220;deepest&#8221; number that can be incremented</li>
<li>Increment it by one</li>
<li>Set any &#8220;deeper&#8221; numbers to one plus the previous number</li>
</ul>
<p>The &#8220;depeest&#8221; number would be the last digit.  In the set {1, 2, 3, 4}, the fourth position would be considered deepest.  Here&#8217;s the first step &#8211; and the most basic increment to do.</p>
<pre>1 2 3 4
1 2 3 5</pre>
<p>The fourth position, &#8217;4&#8242;, is the deepest.  It can be incremented.  We increment it by one.  There are no deeper numbers, so we&#8217;re done.</p>
<p>Let&#8217;s say the fourth position is maxed out.  What happens?</p>
<pre>1 2 3 11
1 2 4 5</pre>
<p>We move back on position to the third position.  This one, &#8217;3&#8242;, is the deepest number that can be incremented.  We increment it by one to 4.  Then we go down the line and set each number after it to one more than the previous.  In this case we only have to set the fourth digit to one more than the third, which is 5.</p>
<p>Let&#8217;s take a look at one more example.</p>
<pre>1 2 10 11
1 3 4 5</pre>
<p>Same thing here.  We move back to the second digit to find one we can increment.  We increment it.  We then drag the others back to be one greater than it.</p>
<h4>Writing This in Code</h4>
<p>The following code should go in the <code>else { }</code> area of the function above.</p>
<p>The first thing we need to do is find the deepest number that can be incremented.  The deepest digit can&#8217;t be incremented when it equals $total.  The second deepest digit can&#8217;t be incremented when it equals ($total &#8211; 1).  The next one at ($total &#8211; 2), then ($total &#8211; 3), etc.</p>
<p>This loop puts that pattern into practice.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$moveBack</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$combo</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$choose</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$moveBack</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$total</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$moveBack</span><span style="color: #009900;">&#41;</span> 
             <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$moveBack</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$choose</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$moveBack</span><span style="color: #339933;">++;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>At the end, $moveBack will hold the number of spaces from the end of the deepest digit.  If $moveBack == 0, then the deepest digit can be incremented.  If $moveBack == 1, we have to move up one digit to increment.  So on and so forth.</p>
<p>If $moveBack == $choose, we can stop counting.  That means that no more digits can be incremented &#8211; we&#8217;ve counted all the way back to digit #0.</p>
<h4>Incrementing to the Next Set</h4>
<p>Now that we know which digit can be incremented, we can increment the whole set and return the array.</p>
<p>First, we should check if $moveBack == $choose (to see if we&#8217;re done).  If it doesn&#8217;t, we start incrementing.</p>
<p>We start by incrementing $combo[$choose - $moveBack].  Then we loop through, decrementing $moveBack until it is 0 &#8211; and we update the deepest digit.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$moveBack</span> <span style="color: #339933;">!=</span> <span style="color: #000088;">$choose</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">//  Increment the deepest digit that can be incremented</span>
  <span style="color: #000088;">$combo</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$choose</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$moveBack</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">++;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//  Loop through and set all other numbers based on this</span>
  <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$moveBack</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$moveBack</span><span style="color: #339933;">--;</span>
    <span style="color: #000088;">$combo</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$choose</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$moveBack</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$combo</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$choose</span> <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$moveBack</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>			
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//  $combo now holds the next combination</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$combo</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">//  We couldn't increment.  The set passed</span>
  <span style="color: #666666; font-style: italic;">//    to the function was the last possible set.</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>	
<span style="color: #009900;">&#125;</span></pre></div></div>

<h4>And&#8230; We&#8217;re Done</h4>
<p>If you put all the pieces together correctly, you should now have a function that iterates through all of the possible combinations given a binomial coefficient.</p>
<p>If you were handling large sets (millions of possible combinations), you&#8217;d probably want to re-write this in a compiled language.  However, the logic is simple and based simply on arrays &#8211; so it should be easily portable to any language.</p>
<p>A word of warning about storing the information.  Don&#8217;t try to store all of the possible combinations in a giant array.  It&#8217;ll choke up all your RAM and eventually kill the script.  I forget how many combinations I tried to calculate that way, but PHP maxed out at 130mb of RAM and died.</p>
<p>Your best bet would be to write the combinations to a data file or a database as you go.  This dumps the storage requirements into a more capable format.</p>
<p>So what do you do with this?  I&#8217;m not really sure.  I wrote the script for a guy who was working on a statistical program he was using for his doctoral thesis.  I&#8217;m sure there are other specialized purposes for the script &#8211; statistics and probability.</p>
<p>If nothing else, it&#8217;s an interesting look at how to write a computer program to tackle a complex math problem.</p>
<p>Here&#8217;s the full source of the script, if you had trouble putting the pieces together.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> getNextCombo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$combo</span><span style="color: #339933;">,</span> <span style="color: #000088;">$total</span><span style="color: #339933;">,</span> <span style="color: #000088;">$choose</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$combo</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//  Return the first set, i.e. {1, 2, 3, 4}</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$choose</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$combo</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$x</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$combo</span><span style="color: #339933;">;</span>    
  <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$moveBack</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$combo</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$choose</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$moveBack</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$total</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$moveBack</span><span style="color: #009900;">&#41;</span> 
             <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$moveBack</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$choose</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$moveBack</span><span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$moveBack</span> <span style="color: #339933;">!=</span> <span style="color: #000088;">$choose</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//  Increment the deepest digit that can be incremented</span>
    <span style="color: #000088;">$combo</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$choose</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$moveBack</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">++;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//  Loop through and set all other numbers based on this</span>
    <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$moveBack</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$moveBack</span><span style="color: #339933;">--;</span>
      <span style="color: #000088;">$combo</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$choose</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$moveBack</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$combo</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$choose</span> <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$moveBack</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>			
    <span style="color: #009900;">&#125;</span>
    <span style="color: #666666; font-style: italic;">//  $combo now holds the next combination</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$combo</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//  We couldn't increment.  The set passed</span>
    <span style="color: #666666; font-style: italic;">//    to the function was the last possible set.</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>	
  <span style="color: #009900;">&#125;</span>
  <span style="color: #666666; font-style: italic;">//  If we didn't return before, no combination was found</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$combo</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$total</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">11</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$choose</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$combo</span> <span style="color: #339933;">=</span> getNextCombo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$combo</span><span style="color: #339933;">,</span> <span style="color: #000088;">$total</span><span style="color: #339933;">,</span> <span style="color: #000088;">$choose</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">//  Process the combo here</span>
  <span style="color: #666666; font-style: italic;">//  Store it in a file, an array, or something</span>
  <span style="color: #666666; font-style: italic;">//  Or just out put it</span>
  <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$combo</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$number</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$number</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' '</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/28/all-combinations-general-function/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to Generate a List of Possible Combinations</title>
		<link>http://www.earn-web-cash.com/2008/02/26/get-all-combinations/</link>
		<comments>http://www.earn-web-cash.com/2008/02/26/get-all-combinations/#comments</comments>
		<pubDate>Tue, 26 Feb 2008 23:16:53 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[probability]]></category>
		<category><![CDATA[random]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/26/get-all-combinations/</guid>
		<description><![CDATA[In statistics and probability, the combination is something special. There are tons of problems that revolve around how many combinations you can get choosing a certain number of random items from a set. We&#8217;ve developed mathematical formulas to help us determine the number of possible combinations &#8211; and therefore the possibility of getting any single [...]]]></description>
			<content:encoded><![CDATA[<p>In statistics and probability, the combination is something special.  There are tons of problems that revolve around how many combinations you can get choosing a certain number of random items from a set.</p>
<p>We&#8217;ve developed mathematical formulas to help us determine the number of possible combinations &#8211; and therefore the possibility of getting any single combination &#8211; but how can we generate a list of the actual combinations?<br />
<span id="more-161"></span></p>
<h4>Find the Number of Combinations: the Binomial Coefficient</h4>
<p>Previously, we looked at <a href="http://www.earn-web-cash.com/2008/02/24/combinations-binomial-coefficients/">how to find the total number of combinations</a>.</p>
<p>Mathematically, we can use the binomial coefficient to figure this out.  With a given set size (i.e. 52 cards) and a given sample size (i.e. 7 cards per hand), we can write the equation &#8211; (52 Choose 7).  We could then re-write the equation as (52! / (7! * 45!) ).</p>
<p>In order to calculate this, we found the most efficient way was to construct two loops.  The first loop calculated the numerator &#8211; after having canceled out all of the numbers contained in 45!.  We then calculated the numerator by looping through 7!.  With simple division, we come up with the answer.</p>
<h4>Listing the Actual Combinations</h4>
<p>A more interesting task, though, is to figure out how to generate a list of these combinations.  To do that, let&#8217;s take a look at a manually generated list &#8211; (6 Choose 3) &#8211; and look for a pattern.</p>
<p>Here&#8217;s the first few combinations&#8230;</p>
<pre>123
12 4
12  5
12   6
1 34
1 3 5
1 3  6
1   45
1   4 6
1    56
 234
 23 5</pre>
<p>You&#8217;ll notice that there&#8217;s a pattern and that the combinations seem to be developing sequentially.  If we can understand that pattern and write a script to repeat it, we&#8217;ll have done our job.</p>
<p>Here&#8217;s the gist of the pattern.  We look at the last number.  If we can increment that, we do.  If not, we look back one number.  If we can increment, we do.  If we increment that number, we &#8220;rewind&#8221; the next number to be equal to it plus one.</p>
<p>For example, look at this series.</p>
<pre>12   6
1 34</pre>
<p>We started by incrementing the last number until it maxed out at 6.  Then we moved back one space, incremented that number, and set the other number equal to one greater than that.  The same thing happens when the last two numbers max out.</p>
<pre>1   56
 234</pre>
<p>You move back to the number that can be incremented, increment it, and then set the other numbers to one greater than the number incremented.</p>
<h4>Using Hard-coded Loops</h4>
<p>The easy (and cheating) way to do this is to hard code a series of nested loops that follow this pattern.</p>
<p>The general formula is (k Choose n) &#8211; where k is the total number of items in the set and n is the sample size being taken.  We will need to create <strong>n</strong> nested loops &#8211; represent each item being chosen in our sample set.</p>
<p>The outer loop will go from <strong>1</strong> to <strong>k &#8211; (n &#8211; 1)</strong>.  Why?  Because at <strong>k &#8211; (n &#8211; 1)</strong>, we can&#8217;t increment the outer number anymore.  Imagine the real numbers are (6 Choose 3).  Once the outer loop gets to 4, we won&#8217;t be able to increment it any further &#8211; we&#8217;ll be at the final combination, 4, 5, 6.</p>
<p>The inner loop will have a beginning value that depends on the outer loop.  If the outer loop is in its first iteration, the first inner loop starts at 2.  When the outer loop is in its second iteration, the first inner loop starts at 3.  It&#8217;s always one greater.  It&#8217;s maximum value is always the same &#8211; <strong>k &#8211; (n &#8211; 2)</strong> &#8211; for the same reason we created the upper limit for the outer loop.</p>
<p>This continues until we get to the inner-most loop.</p>
<p>Here&#8217;s what these nested loops might look like in PHP, generating the combinations for (6 Choose 3).</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$a</span> <span style="color: #339933;">&lt;=</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">6</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$a</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$b</span> <span style="color: #339933;">&lt;=</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">6</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$b</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$c</span> <span style="color: #339933;">&lt;=</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">6</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$c</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$a</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$b</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$c</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>You&#8217;ll see that that script outputs the correct 20 combinations for (6 Choose 3).</p>
<h4>Can We Improve On That?</h4>
<p>Surely we can.  The main problem here is that we&#8217;ve hard-coded the loops.  This is ok for a short-term solution and a small number of items being chosen.</p>
<p>What if you had to choose twenty items from a larger set?  You&#8217;d have to hard-code twenty loops.  It makes the code a bit more unseemly.  This is also inflexible &#8211; you&#8217;ll have to change the code every time you have a different problem.</p>
<p>Instead, we need to look for a generalizable solution.  This is always the best bet, but sometimes it can be tempting to look for a one-off short term solution because it&#8217;s more readily visible.</p>
<p>There is, however, a generalizable solution to this problem.  The key lies in creating a function that finds the next combination using the sequential method outlined above.  It takes a combination and returns a combination &#8211; and you can continuously call the function to iterate through all of the combinations.</p>
<p>Think on that for a while.  It&#8217;s an interesting challenge.  I&#8217;ll explain a <a href="http://www.earn-web-cash.com/2008/02/28/all-combinations-general-function/">solution to it later in the week</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/26/get-all-combinations/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Getting Combinations: Calculating a Binomial Coefficient</title>
		<link>http://www.earn-web-cash.com/2008/02/24/combinations-binomial-coefficients/</link>
		<comments>http://www.earn-web-cash.com/2008/02/24/combinations-binomial-coefficients/#comments</comments>
		<pubDate>Sun, 24 Feb 2008 20:52:50 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Game Design]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[Sundry Musings]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[probability]]></category>
		<category><![CDATA[random]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/24/combinations-binomial-coefficients/</guid>
		<description><![CDATA[I came across this problem on the DigitalPoint forums the other day. How can we write a script to calculate the total number of random combinations in a set (i.e. the binomial coefficient), and how can we generate a list of these combinations? Today, we&#8217;ll tackle the first part. We&#8217;ll start with a quick primer [...]]]></description>
			<content:encoded><![CDATA[<p>I came across this problem on the DigitalPoint forums the other day.  How can we write a script to calculate the total number of random combinations in a set (i.e. the binomial coefficient), and how can we generate a list of these combinations?</p>
<p>Today, we&#8217;ll tackle the first part.  We&#8217;ll start with a quick primer on math &#8211; what is a binomial coefficient?  Then, we&#8217;ll look into the most efficient way to calculate that and get the total number of possible combinations.<br />
<span id="more-159"></span><br />
<h4>What Is a Binomial Coefficient?</h4>
<p>If it&#8217;s been a while since you took statistics or calculus, you might be scratching your head thinking about what a binomial coefficient is and what it has to do with combinations.</p>
<p>If you&#8217;re looking at a set of random items &#8211; let&#8217;s say 52 playing cards &#8211; and you want to pick out a number of them &#8211; let&#8217;s say a 7 card poker hand &#8211; the binomial coefficient tells you how many possible combinations you can make.</p>
<p>The general formula for that is usually written:</p>
<p><code>(x Choose y) = (x!) / (y! * (x-y)!)</code></p>
<p>In our example above, the total number of combinations you can get in a 7-card poker game is (52! / (7! * 45!)) or somewhere around 130,000,000.</p>
<h4>How Do We Calculate That (in PHP)?</h4>
<p>These methods of calculation will work for any language, really.  We&#8217;ll just use PHP as an example.</p>
<p>The simplest way to calculate the binomial coefficient would be to loop through, calculate each factorial, and then evaluate the equation.  Notice I said simple, not efficient.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">52</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$y</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">7</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$num</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//  This is the numerator, 52!</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$a</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span> <span style="color: #000088;">$a</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$num</span> <span style="color: #339933;">*=</span> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$denOne</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//  This is the first part of the denominator, 7!</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$b</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$y</span><span style="color: #339933;">;</span> <span style="color: #000088;">$b</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$denOne</span> <span style="color: #339933;">*=</span> <span style="color: #000088;">$b</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$denTwo</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//  This is the second part of the denominator, 45!</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$c</span> <span style="color: #339933;">&lt;=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$c</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$denTwo</span> <span style="color: #339933;">*=</span> <span style="color: #000088;">$c</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$combinations</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$num</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$denOne</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$denTwo</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>PHP will evaluate this properly because it&#8217;s nice and flexible with data types.  Do you know how large 52! is?  Try 8.06581751709E+67 large.</p>
<p>This is clearly too big to be an integer value, but PHP quietly converts the values to floats and keeps running.  There should be a better way &#8211; one where we don&#8217;t have to calculate all of the factorials.</p>
<h4>Method Two &#8211; Using array_diff</h4>
<p>This method is a bit more along the lines of human logic.  However, it&#8217;s still not going to be efficient.  Although it&#8217;ll work with smaller numbers than the first method, it&#8217;ll take more time and memory to compute.</p>
<p>Basically, we&#8217;ll create three arrays.  One for each set &#8211; {1, 2, 3, &#8230; 52}, {1, 2, 3, &#8230; 7}, {1, 2, 3, &#8230; 45}.</p>
<p>Since we know that the third set (45!) will be in the denominator and the first set (52!) will be in the numerator, we can use a nifty function called array_diff to cancel out all of the common values and shrink the numerator for us.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$newNumerator</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_diff</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$oldNumerator</span><span style="color: #339933;">,</span> <span style="color: #000088;">$oldDenominator</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Then we can calculate the factorials by multiplying all of the values in the array.  We can also make use of another PHP function here &#8211; array_product.  It does more or less what it sounds like &#8211; it finds the product of all the values in an array.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$num</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$a</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span> <span style="color: #000088;">$a</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$num</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// the numerator array, 52!</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$denOne</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$b</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$y</span><span style="color: #339933;">;</span> <span style="color: #000088;">$b</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$denOne</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$b</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//  The first denominator array, 7!</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$denTwo</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$c</span> <span style="color: #339933;">&lt;=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$c</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$denTwo</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$c</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// The second denominator array, 45!</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//  Use array_diff to cancel out the common values in 52! and 45!</span>
<span style="color: #000088;">$num</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_diff</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$num</span><span style="color: #339933;">,</span> <span style="color: #000088;">$denTwo</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$combinations</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_product</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$num</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #990000;">array_product</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$denOne</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This keeps the numbers much smaller.  Therefore we can work with bigger combinations before PHP putts out.  However, the arrays can eat up a lot of memory and processing power&#8230; so there still should be a better way.</p>
<h4>Combine the Two Concepts</h4>
<p>The more efficient way is to combine the two concepts.  We&#8217;ll use the first method of multiplying through a loop to generate the factorials.  This is far more efficient than iterating an array.</p>
<p>However, we&#8217;ll still cancel out the common values between 52! and 45!.  To do this, we modify the first loop so that it counts down from $x (52) to $x &#8211; $y (45).  Then we simply ignore the third loop &#8211; since it&#8217;s values were essentially counted when we calculated the first loop.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$num</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span> <span style="color: #000088;">$a</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$y</span><span style="color: #339933;">;</span> <span style="color: #000088;">$a</span><span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$num</span> <span style="color: #339933;">*=</span> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$den</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$b</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$y</span><span style="color: #339933;">;</span> <span style="color: #000088;">$b</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$den</span> <span style="color: #339933;">*=</span> <span style="color: #000088;">$b</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$combinations</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$num</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$den</span><span style="color: #339933;">;</span></pre></div></div>

<p>At this point, our script is pretty efficient.  It doesn&#8217;t use up a lot of memory, it executes quickly, and it&#8217;s not calculating very many redundant values.</p>
<p>However, there&#8217;s still a limit to what we can do.  The biggest number PHP seems to be able to calculate is 170!.  After PHP sees the value 7.25741561531E+306 it just seems to give up.  Geez, like 300 zeroes is a lot.</p>
<p>You could improve this a bit more, and increase the number of combinations it could compute.  Rather than calculating the $numerator and $denominator separate, as we did here, you&#8217;d want to calculate the $denominator first and then cancel out values as you multiply the numerator.</p>
<p>In other words, calculate the $denominator as normal.  Then, while you&#8217;re looping through to calculate the numerator, you factor out any common values in the $denominator and the number about to be multiplied into the numerator.</p>
<h4>Who Cares About How Many Combinations&#8230; I Want to See Them</h4>
<p>This is really the boring part.  Who cares how many combinations there are?  We can easily find that with a calculator.</p>
<p>The interesting part &#8211; where we definitely need a computer to help us &#8211; is to iterate through the set and find all of the possible combinations.  We&#8217;ll come back to this later in the week.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/24/combinations-binomial-coefficients/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Javascript Function: Random Number Generator</title>
		<link>http://www.earn-web-cash.com/2008/02/24/random-number-function/</link>
		<comments>http://www.earn-web-cash.com/2008/02/24/random-number-function/#comments</comments>
		<pubDate>Sun, 24 Feb 2008 20:07:28 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Game Design]]></category>
		<category><![CDATA[JS Tutorials]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[random]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/24/random-number-function/</guid>
		<description><![CDATA[Yesterday, I wrote a short article on generating random numbers in Javascript. The Javascript random number generator is similar to C/++ &#8211; it gives you a random number from 0 to 1 and you need to make it useful. At the suggestion of a reader (thanks Justin), I decided to go ahead and turn the [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday, I wrote a short <a href="http://www.earn-web-cash.com/2008/02/23/random-number-javascript/">article on generating random numbers in Javascript</a>.  The Javascript random number generator is similar to C/++ &#8211; it gives you a random number from 0 to 1 and you need to make it useful.</p>
<p>At the suggestion of a reader (thanks Justin), I decided to go ahead and turn the general formula for this into a short helper function to generate a random number.<br />
<span id="more-158"></span></p>
<h4>Random Numbers: General Formula</h4>
<p>First, let&#8217;s take a minute and recap the general formula for finding a random number.</p>
<p>Javascript&#8217;s Math.random() method returns a random number between 0 and 1.  By multiplying this decimal value by a whole number, we can generate a desired range of values.  For example, by multiplying Math.random()&#8217;s result by 10, we get a range of 0 to 10.</p>
<p>Next, we take the Math.floor value of that random number.  This gives us just the integer portion &#8211; and it effectively cuts our range to 0 through 9.  Finally, we can modify this by adding an offset number.  So if we wanted a random number between 51 and 60, we&#8217;d add 51 to that result.</p>
<h4>Creating a Random Number Function</h4>
<p>The basic formula we end up with is&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span> Math.<span style="color: #660066;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> setSize<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> setOffset<span style="color: #339933;">;</span></pre></div></div>

<p>Our code would be much more readable if we had a helper function that could be called like this&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">randomNumber <span style="color: #339933;">=</span> rand<span style="color: #009900;">&#40;</span>min<span style="color: #339933;">,</span> max<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Our function is going to take two parameters &#8211; min and max.  From these two parameters, we can calculate the offset and the set size and generate the proper random number.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> rand<span style="color: #009900;">&#40;</span>min<span style="color: #339933;">,</span> max<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> offset <span style="color: #339933;">=</span> min<span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> range <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>max <span style="color: #339933;">-</span> min<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #003366; font-weight: bold;">var</span> randomNumber <span style="color: #339933;">=</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span> Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> range<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> offset<span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">return</span> randomNumber<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>All done.</p>
<p>Add that function to a utility file, and you&#8217;ve got a handy random number generator at your disposal.  It even uses the same format as PHP&#8217;s rand() function, to help increase consistency and readability in your code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/24/random-number-function/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>How to Create a Random Number in Javascript</title>
		<link>http://www.earn-web-cash.com/2008/02/23/random-number-javascript/</link>
		<comments>http://www.earn-web-cash.com/2008/02/23/random-number-javascript/#comments</comments>
		<pubDate>Sun, 24 Feb 2008 04:34:19 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Game Design]]></category>
		<category><![CDATA[JS Tutorials]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[random]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/23/random-number-javascript/</guid>
		<description><![CDATA[I've been working with PHP for some time now, and I've become spoiled with the rand() function for generating random numbers.  It's so simple - pass a min, pass a max, and get the number you want.

While I was working on various game-related scripts this weekend, I was left wondering how to create a random number in Javascript.  It's not quite as simple as the rand() function - we'll have to go back to the old C-style random number generation.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working with PHP for some time now, and I&#8217;ve become spoiled with the rand() function for generating random numbers.  It&#8217;s so simple &#8211; pass a min, pass a max, and get the number you want.</p>
<p>While I was working on various game-related scripts this weekend, I was left wondering how to create a random number in Javascript.  It&#8217;s not quite as simple as the rand() function &#8211; we&#8217;ll have to go back to the old C-style random number generation.<br />
<span id="more-156"></span></p>
<h4>The Math Object and Random Numbers</h4>
<p>Javascript comes with a built-in random number generator as part of the Math object.  This Math object does a number of calculations &#8211; trig functions (sine, cosine, and tangent), rounding, logarithms, exponents, etc.</p>
<p>To use the Math class to generate a random number, you begin with the random method.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This method returns a random value &#8211; between 0 and 1.</p>
<h4>How Can I Make a Useful Random Number?</h4>
<p>If you&#8217;ve ever programmed in C/C++, you&#8217;ll know what to do.  If you&#8217;re used to working with something like PHP &#8211; which comes with an intelligent built-in random number generator &#8211; you may be left in the dark.</p>
<p>How can you take a random decimal &#8211; between 0 and 1 &#8211; and turn it into a random number between, say, 1 and 100?</p>
<p>The answer is simple &#8211; math.  You need to multiply the random() result to get a certain sized range and then use addition to start the range at the right number.</p>
<p>Let&#8217;s say you multiply the result of Math.random() by 10.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">10</span></pre></div></div>

<p>Since the results of Math.random() are between 0 and 1, the results of this statement will be 0 to 10.  The minimum result is (0 * 10 = 0) and the highest result is (1 * 10 = 10).</p>
<p>If you were to only look at the whole number part of the result, you would get a range of 0 to 9.  In actuality, the result of Math.random() is never 0 or 1, so the above statement won&#8217;t give you a result of 10.</p>
<p>Therefore we can use the whole number portion of the result to get a random integer &#8211; 0 to 9.</p>
<h4>How Do We Get a Whole Number?</h4>
<p>Simple.  Use the floor method of the Math class.  Math.floor returns the whole number portion of a float/decimal.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1.45</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//  Result is 1</span></pre></div></div>

<p>Simply pass a float/decimal to the Math.floor method, and it will return the integer portion of that number.  If you&#8217;re working with negative numbers, it will always round down.  Keep that in mind.  For positive numbers, it&#8217;s simply a truncation function.</p>
<p>To get a random integer, we simply pass the result of Math.rand() to Math.floor().</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span> Math.<span style="color: #660066;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h4>What If My Random Set Doesn&#8217;t Start at 0?</h4>
<p>Here&#8217;s the last piece of the random puzzle.</p>
<p>You can determine the size of your random number set with multiplication.  The coefficient you use (like we used 10 before) determines the range your random number can be in.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span> Math.<span style="color: #660066;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 10 Numbers, 0-9</span>
Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span> Math.<span style="color: #660066;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 5 Numbers, 0-4</span>
Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span> Math.<span style="color: #660066;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 100 Numbers, 0-99</span></pre></div></div>

<p>If the set you want to create a random number from doesn&#8217;t start with 0, you simply use addition to get what you want.</p>
<p>Let&#8217;s say you want a random number from 101-150.  Your set contains 50 numbers, and it starts with 101.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span> Math.<span style="color: #660066;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">50</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">101</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 101 - 150</span></pre></div></div>

<p>The basic statement &#8211; Math.floor( Math.rand() * 50) &#8211; returns a value between 0 and 49.  We  want to offset that by 101 places and we&#8217;ll have our desired random set &#8211; 101 to 150.</p>
<p>At this point, you should have a general equation you can use for generating random numbers from any given set.  Take Math.rand(), apply a coefficient to determine the size of the random number set, and then use addition to offset that number to your desired starting point.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span> Math.<span style="color: #660066;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> setSize<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> setOffset<span style="color: #339933;">;</span></pre></div></div>

<p>Now go find something useful to use these random numbers for.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/23/random-number-javascript/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

