Last active
December 28, 2015 15:29
-
-
Save mtmzorro/7521559 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
/** | |
* [getHashProbability hash概率] | |
* @param {[String]} strNum [源数据] | |
* @param {[Number]} baseNum [基数 分母] | |
*/ | |
function getHashProbability(strNum, baseNum){ | |
// hash算法 | |
function hashCode(str) { | |
for (var result = 0, i = 0; i < str.length; i++) { | |
result = (result << 5) - result + str.charCodeAt(i); | |
result &= result; | |
} | |
return result; | |
} | |
return Math.abs(hashCode(strNum)) % baseNum; | |
} | |
/** | |
* [demo 求一个数据在 10000为基数的区间中概率命中] | |
* 预想结果是将这组数据分为 0-499 500-999 1000-9999 3组 | |
* 即实现 5% 5% 90% 分布 | |
*/ | |
var flag = getHashProbability("12312312312", 10000); | |
if (flag < 500) { | |
console.log('verson1'); | |
}else if(flag >= 500 && flag < 1000){ | |
console.log('verson2'); | |
}else { | |
console.log('verson3'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment