Monday, June 27, 2011

Simple javascript explanation of Monty Hall's game show problem


I have created a simple javascript program that will demonstrate how you can increase your probability of winning in the "Monty Hall's Let's Make A Deal game show"


 <script type="text/javascript">  
      var samplesize = 1000;  // Sample size
      var shuffle = function(v){  
           for(var j, x, i = v.length; i; j = parseInt(Math.random() * i), x = v[--i], v[i] = v[j], v[j] = x);  
           return v;  
      };  
      var a=[0,0,1]  

      var randomIndex;  
      var randomIndex1,randomIndex2,randomIndex3;  
      var randomDraw;  
      var result="";  
      var switched=0;  
      var won=0;  
      var switchedAndWon = 0;  
      var switchedAndLost=0;  
      var keptAndWon=0;  
      var keptAndLost=0;  
      var hasswitched;  
      var haswon;  
      for(var i=0; i<samplesize; i++){  
           // Step 1: Shuffle it first  
           a = shuffle(a);  
           // Step 2: Your first draw:   
           randomIndex1 = Math.floor(Math.random()*3);  
           randomDraw = a[randomIndex1];  
           // Step 3: Monty's draw:   
           randomIndex2 = -1;  
           while(randomIndex2 == randomIndex1 || randomIndex2 == -1 || a[randomIndex2] == 1){  
                randomIndex2 = Math.floor(Math.random()*3);  
           }  
           randomDraw = a[randomIndex2];  
           // Step 4: Your second draw:   
           randomIndex3 = -1;  
           while(randomIndex3 == randomIndex2 || randomIndex3 == -1){  
                randomIndex3 = Math.floor(Math.random()*3);  
           }  
           randomDraw = a[randomIndex3];  
           // Step 5: Now calculate  
           hasswitched = (randomIndex1 != randomIndex3);  
           haswon = randomDraw;  
           if(hasswitched){  
                switched++;  
           }  
           if(haswon){  
                won++;  
           }  
           if(hasswitched && haswon){  
                switchedAndWon++;  
           }else if(hasswitched && !haswon){  
                switchedAndLost++;  
           }else if(!hasswitched && haswon){  
                keptAndWon++;  
           }else if(!hasswitched && !haswon){  
                keptAndLost++;  
           }  
           //Push the results:  
           result += "["+String(a)+"] You: "+randomIndex1+", Monty: "+randomIndex2+", You: "+randomIndex3+"(switched: "+(randomIndex1 != randomIndex3)+")<br/>";  
      }  
      //document.write("<br/>switchedAndWon = "+switchedAndWon+" of total "+samplesize+" results");  
      document.write("<br/>switchedAndWon = "+switchedAndWon+" (There! Almost twice of keptAndWon)");  
      document.write("<br/>switchedAndLost = "+switchedAndLost);  
      document.write("<br/>keptAndWon = "+keptAndWon);  
      document.write("<br/>keptAndLost = "+keptAndLost);  
      document.write("<br/><br/>");  
      document.write(result);  
 </script>  

I hope you enjoy it.

Tuesday, June 07, 2011

Get Buzz feed into Facebook via feedburner

Many people simultaneously use multiple social networking sites like twitter, facebook, google buzz, orkut etc.  I too use most of such sites. 

But gradually it becomes really painful of keep track of each and every site and you end up concentrating more on one particular site while ignoring others. 

While Buzz allows you to import your feeds from twitter, blogs etc; it doesn't have any option to import facebook wall posts. 
On the other hand, though facebook does allow you to import feeds of your own blog, it doesn't allow you to import Buzz feeds which is quite irksome for a user like me who uses Buzz heavily and want all my buzzes to be reflected on Facebook wall. 

Thankfully, feedburner and facebook's notes come to your rescue. 

Follow these steps to import your google buzz posts into facebook notes (and in turn, facebook wall)

1. Generate google buzz feeds using the following url
http://buzz.googleapis.com/feeds/{user}/public/posted

2. Login to feedburner.com using your google login and burn (the way they call it) the above feed. Feedburner will create a public feed url something like
http://feeds.feedburner.com/googleapis/WXYZ

3. Login to facebook and click on notes in the left manu. 
4. In facebook notes, click on "Edit import settings". 


5. On the next page, enter the feedburner url and click on Start Importing. 
Note that for some reason, facebook doesn't allow direct import of Buzz feeds from buzz.googleapis.com. That is why we have to "burn" the feeds using feedburner. 

6. Click on Confirm Importing after you successfully preview the imported posts. 

Now click on either notes or your facebook wall and you'll see that all your buzz posts imported there. 

Monday, June 06, 2011

Nostalgia 1997

As India is going to play its first ODI (I'm least interested in the series as there is neither Tendulkar nor Sehwag) with WI today, i quickly recalled that the tour was broadcast live by ESPN and the lyrics went like this:

"Pepsi presents India on tour, Calypso series; 
in association with
Hero Honda, leading the way!
Thomson, the bigger picture, the better picture!
and Anubhav group, the people care for the environment!"

Anyone recalling this?