Skip to content

Instantly share code, notes, and snippets.

@xsurge83
Created May 10, 2014 08:47
Show Gist options
  • Save xsurge83/48f3b5ceaca904ea51cf to your computer and use it in GitHub Desktop.
Save xsurge83/48f3b5ceaca904ea51cf to your computer and use it in GitHub Desktop.
Distribution Probability
function randomProb(hash, n){
var bucketHash = {}, bucketHash, index, key,
sum=0, results =[], randValue, curBucketValue;
for(key in hash){
sum += hash[key];
bucketHash[key] = sum;
}
for (var index = 0; index < n; index++) {
randValue = Math.random();
for(key in bucketHash){
curBucketValue = bucketHash[key];
if(curBucketValue>randValue){
results.push(key);
break;
}
}
};
return results;
}
var hash = { 1 : 0.5 , 2 : 0.5};
console.log('%j', randomProb(hash, 4));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment