好吧,您可以尝试使用指数失真而不是线性失真。
在此处查看 JavaScript 中的实现:http: //jsfiddle.net/hua7R/1/
首先我们设置级别数和指数失真:
var levels=12,
distortion=5;
以下函数并不重要,我只是为了让日志按列分布而使用它。
function numToStr(n){
var s=String(n);
for(var i=s.length;i<3;i++){
s=' '+s;
}
return s;
}
我们遍历所有级别以查看它们之间的差异:
for(var l=0;l<=levels;l++){
//In this test the right solution is always the last:
var percentages=[15,15,15,55];
/*The following function gives us a random number from 0 (included)
to `percentage`'s lenght (not included).
If it's called with the argument `true`, it checks if we can
subtract 1 to that percentage (we don't want negative percentages).*/
function random(cond){
var i=Math.floor(Math.random()*percentages.length);
if(!cond||percentages[i]>0){
return i;
}
return random(true);
}
/*Then we iterate from 0 to the maximum value between `l*10`
(`l` is the current level) and `l` raised to the power of `distortion`*/
for(var i=0;i<Math.max(l*10,Math.pow(l,distortion));i++){
percentages[random(true)]--;
percentages[random()]++;
}
//Finally, the log:
document.getElementById('log').innerHTML+="level "+numToStr(l)+" -> ["+numToStr(percentages[0])+", "+numToStr(percentages[1])+", "+numToStr(percentages[2])+", "+numToStr(percentages[3])+"]\n";
}