Skip to content

Instantly share code, notes, and snippets.

@benzhe
Created July 21, 2012 06:50
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 benzhe/3154903 to your computer and use it in GitHub Desktop.
Save benzhe/3154903 to your computer and use it in GitHub Desktop.
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