Created
May 10, 2014 08:47
-
-
Save xsurge83/48f3b5ceaca904ea51cf to your computer and use it in GitHub Desktop.
Distribution Probability
This file contains hidden or 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
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