Created
July 21, 2012 06:50
-
-
Save benzhe/3154903 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var nums = [0,0,0,0], | |
n = 1000000; | |
var rand=function(){return parseInt(Math.random()*10)}; | |
function jiaoji_count(val){ | |
var count = 0,a=val[0],b=val[1]; | |
for(var m = 0;m<a.length;m++){ | |
for(var n=0;n<b.length;n++){ | |
if(a[m] == b[n]){count++;b.splice(n,1);break;}}} | |
return count; | |
} | |
for(var i = 0; i<n; i++){ | |
var count = jiaoji_count([[rand(),rand(),rand()],[rand(),rand(),rand()]]); | |
switch(count){ | |
case 0:nums[0]++;break;case 1:nums[1]++;break; | |
case 2:nums[2]++;break;case 3:nums[3]++; | |
} | |
} | |
nums.forEach(function(val){ | |
console.log(val/n); | |
}) | |
//结果是 | |
//0.391813 | |
//0.478164 | |
//0.124926 | |
//0.005097 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment