<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF xmlns="http://purl.org/rss/1.0/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <channel rdf:about="http://www.benmeadowcroft.com/rss/weblog.rdf" xmlns="">
    <title>BenMeadowcroft.com</title>
    <description>Syndicated content from Ben Meadowcrofts website, talks a lot about programming and internet technologies like cascading style sheets, XHTML, XML and RSS.</description>
    <dc:language>en-GB</dc:language>
    <sy:updatePeriod>daily</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>

    <link>http://www.benmeadowcroft.com</link>
    <items>
      <rdf:Seq>
      	<li rdf:resource="http://www.benmeadowcroft.com/me/archive/2005/august/#link_10th_1" />
      </rdf:Seq>
    </items>
  </channel>

<item rdf:about="http://www.benmeadowcroft.com/me/archive/2005/august/#link_10th_1" xmlns="">
	<title xml:lang="en-GB">Random &#960;</title>
   <link>http://www.benmeadowcroft.com/me/archive/2005/august/#link_10th_1</link>
   <content:encoded xml:lang="en-GB"><![CDATA[
	<div class="blogitem"><h3><a href="http://www.benmeadowcroft.com/me/archive/2005/august/#link_10th_1" title="permanent link" rel="bookmark">Random &#960;.</a> (posted 10th August 2005)</h3>
	<p>The ridiculous fish blog recently posted an entry about <a href="http://ridiculousfish.com/blog/?p=23">using "random" large integers to calculate &#960;</a>. That post was in the context of getting blog commentators to enter two large integer values as a kind of <acronym title="Completely Automated Public Turing Test to Tell Computers and Humans Apart">CAPTCHA</acronym> to keep out spammers automated commenting bots. The idea of calculating pi using just random sequences of integers was intriguing to me so I set about <a href="http://www.benmeadowcroft.com/me/archive/2005/august/pi.shtml">implementing it in JavaScript</a>. I've included the functions used below:</p>

	<p>First a fairly simple function that returns a random positive integer:
	<pre class="blockcode"><code>
function randomPositiveInteger()
{
   return Math.round(Math.random() * (Math.pow(2, 31)-1));
}</code></pre></p>
	<p>Next a JavaScript implementation of the <a href="http://en.wikipedia.org/wiki/Binary_GCD_algorithm">binary greatest common denominator algorithm</a> followed by a simple coprime function that takes two positive integers and determines if they are coprime or not (by determining if the <abbr title="greatest common denominator">gcd</abbr> is 1):
	<pre class="blockcode"><code>
function gcd(u, v)
{
   var k = 0;
   while ((u & 1) == 0  &&  (v & 1) == 0)
   {
      u >>= 1;
      v >>= 1;
      k++;
   }
   do
   {
      if ((u & 1) == 0)
         u >>= 1;
      else if ((v & 1) == 0)
         v >>= 1;
      else if (u >= v)
         u = (u-v) >> 1;
      else
         v = (v-u) >> 1;
   } while (u > 0);
   return v << k;
}

function isCoprime(a, b)
{
   return gcd(a,b) == 1;
}
	</code></pre></p>

	<p>Finally the function that ties it all together; Note the assignment of properties to the function object so that the function can maintain a history of results without having to store variables outside its scope:
	<pre class="blockcode"><code>
function nextPiEstimate()
{
   /* Initialise the properties used to hold the tallies if required. */
   if (nextPiEstimate.totalCoprimePairs == null)
   {
      nextPiEstimate.totalCoprimePairs = 0;
   }
   if (nextPiEstimate.totalPairs == null)
   {
      nextPiEstimate.totalPairs = 0;
   }

   /* Generate two random integers */
   var a = randomPositiveInteger();
   var b = randomPositiveInteger();
   nextPiEstimate.totalPairs++;

   /* Determine if we are dealing with a coprime pair or not */
   if (isCoprime(a,b))
   {
      nextPiEstimate.totalCoprimePairs++;
   }

   /* Determine the percentage of pairs that were prime and calculate Pi */
   var invertedPercentage = nextPiEstimate.totalPairs / nextPiEstimate.totalCoprimePairs;
   var pi = Math.sqrt(invertedPercentage * 6);

   return pi
}
	</code></pre></p>
	<p><a href="http://www.benmeadowcroft.com/me/archive/2005/august/pi.shtml">See the code in action</a></p>
	</div>

	<p class="ahem">If you are interested in downloading a browser that is capable of rendering this page as it was designed visit my <a href="http://www.benmeadowcroft.com/upgrade/" title="Download a browser that complies with Web standards.">Browser Upgrade Initiative page</a>.</p>

</div>
	]]></content:encoded>
</item>


</rdf:RDF>