<?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; math</title>
	<atom:link href="http://www.earn-web-cash.com/tag/math/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>Binary Code and Bitwise Operators (in PHP)</title>
		<link>http://www.earn-web-cash.com/2008/04/06/binary-code-and-bitwise-operators-in-php/</link>
		<comments>http://www.earn-web-cash.com/2008/04/06/binary-code-and-bitwise-operators-in-php/#comments</comments>
		<pubDate>Mon, 07 Apr 2008 02:30:17 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[Redirected]]></category>
		<category><![CDATA[binary]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/04/06/binary-code-and-bitwise-operators-in-php/</guid>
		<description><![CDATA[At it&#8217;s core, all of the information on your computer is made up of bits &#8211; or 0&#8242;s and 1&#8242;s. There&#8217;s quite a bit of interpretation that goes on between that basic binary code and the information as it is displayed on your screen. However, you may find a time to work with data at [...]]]></description>
			<content:encoded><![CDATA[<p>At it&#8217;s core, all of the information on your computer is made up of bits &#8211; or 0&#8242;s and 1&#8242;s.  There&#8217;s quite a bit of interpretation that goes on between that basic binary code and the information as it is displayed on your screen.</p>
<p>However, you may find a time to work with data at the binary level in PHP (and many other programming languages).  Here&#8217;s a quick guide to understanding binary numbers and the common operators for handling them.<br />
<span id="more-185"></span></p>
<h4>&#8220;Normal&#8221; Numbers &#8211; Base Ten, Decimal System</h4>
<p>When you deal with numbers, you are probably using the &#8220;Base Ten&#8221; or &#8220;Decimal&#8221; number system.  These are numbers made up of a set of ten different digits &#8211; zero through nine.</p>
<p>A given quantity &#8211; like 512 &#8211; is created with a series of digits.  The position of the digit tells us how much the digit actually represents.  The &#8217;2&#8242; here is in the units place, so it simply represents 2.  The 1 is in the tens place, so it actually represents 10.  The 5 is in the hundreds place, so it represents 500.</p>
<p>The number could be re-written as:</p>
<pre>500 + 10 + 2 = 512</pre>
<p>You&#8217;ll notice a pattern here.  Each new place is worth ten times the previous place.  The third digit (the hundreds place) is worth 10 * 10.  If we added a fourth place, it would be worth 10 * 100 (1000).</p>
<p>This gives us a nifty little mathematical formula &#8211; which is why this is called &#8220;base ten&#8221; numbering.  The nth digit (where zero is the right-most digit) is worth 10^n.  Here&#8217;s the number 1024 re-written to illustrate this.</p>
<pre>(1 * 10^3) + (0 * 10^2) + (2 * 10^1) + (4 * 10^0) = 1024</pre>
<p>This is a lot of work to read a number that we intuitively know is worth 1,024.  However, it helps to understand how binary numbers work.</p>
<h4>Binary Numbers &#8211; The Base Two System</h4>
<p>While base ten numbers are simple enough for humans to work with, computers are designed to use a binary or base two numbering system.</p>
<p>The simplest unit of information is a binary bit &#8211; a 0 or a 1.</p>
<p>To create complex numbers, we can string together 0&#8242;s and 1&#8242;s.  For example, a number could be written as 0010 0101.</p>
<p>To understand what this value means, we can apply the same rules as we did above.  However, since this is a base two system instead of a base ten system, each place is multiplied by 2^n.</p>
<p>Let&#8217;s start with a smaller example &#8211; 1111.</p>
<pre>(1 * 2^3) + (1 * 2^2) + (1 * 2^1) + (1 * 2^0)
    = 8 + 4 + 2 + 1
    = 15</pre>
<p>These rules can be expanded to represent byte-sized numbers (8-bits, or 2^8, or 255) or even larger numbers.</p>
<h4>Manipulating Bits and Bytes &#8211; Bitwise Operators</h4>
<p>Most programming languages come with some built in operators to handle bits.  These help make very basic comparisons and manipulations of numbers.</p>
<p>These typically include the &#8216;and&#8217; or intersection operator, the &#8216;or&#8217; or union operator, and the &#8216;xor&#8217; or exclusive-or operator.  We&#8217;ll just take a look at the &#8216;and&#8217; and &#8216;or&#8217; operators right now.  [Note:  I use the terms intersection and union because I think they make it much easier to understand, if you are at all familiar with math sets and statistics.  They aren&#8217;t typical &#8216;coding&#8217; terms).</p>
<p>The &#8216;and&#8217; or intersection operator &#8211; the &#8216;&amp;&#8217; character in PHP &#8211; compares two binary numbers and returns a new binary number with a &#8217;1&#8242; in every unit in which <strong>both</strong> numbers have a &#8217;1&#8242;.  If both numbers have a value in the second place, then the return number will have a value in the second place.  If only one number has a value in the third place, then the return number will have no value in the third place.</p>
<pre>0111 &amp; 0101 = 0101
0101 &amp; 1010 = 0000
1111 &#038; 1111 = 1111</pre>
<p>If you imagine &#8217;0111&#8242; and &#8217;0101&#8242; as sets of numbers ({4, 2, 1} and {4, 1} respectively), then the result of a bitwise &#8216;and&#8217; operation is the simple intersection of the two sets.</p>
<p>The bitwise &#8216;or&#8217; operator &#8211; a | in PHP &#8211; returns a value in each place that <strong>either</strong> number has a value.  So if only one number has a value in the first place, the result will have a value in the first place.  A place will only have no value if <strong>neither</strong> number had a value there.</p>
<pre>0111 | 0101 = 0111
0101 1110 | 1010 0001 = 1111 1111
0000 1111 | 1111 0000 = 1111 1111</pre>
<p>These operators aren&#8217;t used very much, but it is important to recognize them.  If you don&#8217;t know what they are, it is easy to confuse them with the standard <a href="http://www.earn-web-cash.com/2008/02/16/php-logical-operator/">logical operators</a> &#8211; || and &amp;&amp;.</p>
<p>For example, the following two lines of code have <strong>very</strong> different meanings.</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;">124</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$y</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">36</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$bitwise</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">&amp;</span> <span style="color: #000088;">$y</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$logical</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$y</span><span style="color: #339933;">;</span></pre></div></div>

<p>In the first comparison ($x &amp; $y), we&#8217;re finding the bitwise intersection of the two values.  This would be&#8230;</p>
<pre>0111 1100 &amp; 0010 0100 = 0010 0100</pre>
<p>In base ten terms, $bitwise would be equal to 36.  In the second example ($x &amp;&amp; $y), we&#8217;re looking for a simple boolean comparison.  In this case, both $x and $y have a value, so the comparison will return &#8217;1&#8242; or &#8216;true&#8217;.</p>
<h4>Is This Useful&#8230;?  Comparing Constant Flags</h4>
<p>You may be thinking to yourself, &#8220;When will I ever use this?&#8221;</p>
<p>While anyone working with computer code should understand how binary numbers work, there will probably be very few cases in which you manipulate numerical values in a binary format.  The rest of the world works with decimal numbers &#8211; and your programming language is designed to do the same thing.</p>
<p>However, there are some cases where you <strong>will</strong> want to use bitwise numbers and comparisons.  A perfect example is the use of constants and flags.</p>
<p>Let&#8217;s say that a function takes 16 parameters.  Each of these parameters is a simple &#8220;True&#8221; or &#8220;False.&#8221;</p>
<p>You could write the function take sixteen different parameters and use sixteen different variables.  However, this would make function calls quite long &#8211; especially if you usually only make two or three parameters true.</p>
<p>Instead, you can pass one value as a parameter.  This value &#8211; 16 bits long &#8211; includes 16 individual flags to tell the function whether a specific parameter is true or false.</p>
<p>If every parameter is set to true, this value would be &#8220;1111 1111 1111 1111&#8243;.  If only the first parameter should be set to true, this value would be &#8220;0000 0000 0000 0001&#8243;.</p>
<p>Each flag is represented by a specific place in this binary number.  The first flag is &#8220;0001,&#8221; the second flag is &#8220;0010,&#8221; the third flag is &#8220;0100,&#8221; etc.</p>
<p>Your code wouldn&#8217;t be very readable if you referred to each flag by its numerical value.  Instead, you can define constant values.  For example, FUNCTION_FLAG_ONE would represent &#8220;0001,&#8221; &#8220;FUNCTION_FLAG_TWO would represent &#8220;0010,&#8221; etc.  These constants would follow whatever naming convention you usually use.</p>
<p>The value of this is that you can easily set any number of flags with the use of the bitwise &#8216;or&#8217; operator.</p>
<p>For example, let&#8217;s say you want to set flags one, seven, and fifteen to true.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$flags</span> <span style="color: #339933;">=</span> FUNCTION_FLAG_ONE <span style="color: #339933;">|</span> 
    FUNCTION_FLAG_SEVEN <span style="color: #339933;">|</span> FUNCTION_FLAG_FIFTEEN<span style="color: #339933;">;</span></pre></div></div>

<p>The result would be &#8217;0100 0000 0100 0001.&#8217;</p>
<p>Inside the function, you could then use the bitwise &#8216;and&#8217; operator to see which flags were set.  For example, this would let you check to see if the fifteenth flag was set to true.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$fifteen</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$flags</span> <span style="color: #339933;">&amp;</span> FUNCTION_FLAG_FIFTEEN<span style="color: #339933;">;</span></pre></div></div>

<p>Although your function&#8217;s code will have to do some legwork to find out which flags were set and which ones were not, this makes the function call itself <strong>much</strong> more efficient and readable.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/04/06/binary-code-and-bitwise-operators-in-php/feed/</wfw:commentRss>
		<slash:comments>0</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>

