Skip to content

Instantly share code, notes, and snippets.

@shimondoodkin
Created March 6, 2012 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shimondoodkin/1987046 to your computer and use it in GitHub Desktop.
Save shimondoodkin/1987046 to your computer and use it in GitHub Desktop.
in game theory there is an idea about the old tv show of 3 doors where you choose a door and then the showman says there is nothing behind that door you did not choose. would you like to switch? the idea saya that if you switch you should have more chance
var doors=7;
function offerdoors()
{
if(doors<3) return "error";
var arr=[];for(var d=0;d<doors;d++)arr[d]=0;// fill the rage
arr[Math.floor(Math.random()*3)]=1;
choose(arr)
}
function whereno(arr,choice)
{
while(true)
{
var randomtoremove=Math.floor(Math.random()*doors);
if(randomtoremove==choice) continue;
if(arr[randomtoremove]==1) continue;
return randomtoremove;
}
}
function choose(arr)
{
var choice=Math.floor(Math.random()*doors);
var whereisno=whereno(arr,choice)
var toswitch=Math.floor(Math.random()*2);
if(toswitch==1)
{
while(true)
{
var newchoice=Math.floor(Math.random()*doors);
if(newchoice==whereisno)continue;
if(newchoice==choice)continue;
break;
}
log_result(toswitch,arr[newchoice])
}
else
log_result(toswitch,arr[choice])
}
var results=[[],[]]
function log_result(toswitch,x)
{
results[toswitch].push(x);
}
var tests=1000
while(results[0].length<tests||results[1].length<tests)
offerdoors()
function round(num, dec) {return Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);}
console.log("percent of success")
var sum,toswitch;
sum=0;
toswitch=1;
results[toswitch].splice(tests);;
results[toswitch].forEach(function(c){sum+=c})
var avg=sum/results[toswitch].length;
console.log(results[toswitch].length+" tests"," to switch",round(avg*100,2)+"%","a chance of "+round(avg*doors,2)+" in "+doors);
sum=0;
toswitch=0;
results[toswitch].splice(tests);;
results[toswitch].forEach(function(c){sum+=c})
var avg=sum/results[toswitch].length;
console.log(results[toswitch].length+" tests","not switch",round(avg*100,2)+"%","a chance of "+round(avg*doors,2)+" in "+doors);
@shimondoodkin
Copy link
Author

sample output:

percent of success
1000 tests to switch 66.9% a chance of 2.01 in 3
1000 tests not switch 34% a chance of 1.02 in 3

@shimondoodkin
Copy link
Author

percent of success
1000 tests to switch 38.2% a chance of 1.53 in 4
1000 tests not switch 23.3% a chance of 0.93 in 4

@shimondoodkin
Copy link
Author

percent of success
1000 tests to switch 11% a chance of 1.1 in 10
1000 tests not switch 10.2% a chance of 1.02 in 10

@shimondoodkin
Copy link
Author

percent of success
1000 tests to switch 0.3% a chance of 0.3 in 100
1000 tests not switch 0.7% a chance of 0.7 in 100

now this is surprising the success supposed to be 0.01; but it is larger.

@shimondoodkin
Copy link
Author

I guess the higher rate is because you choose one from two, not from 3.
a choice of a one from two really makes a difference
if you switch. you get some chance points for free as the removed chance.
as 1/[num choices], when the chance is 1/3, you get another 1/3 for free.
but in numbers larger then 5 it halps a very little.
looks like a chance of 50% that it will halp a about 10% in larger numbers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment