我希望生成14个数字(具有正态分布,负偏斜和正偏斜),Sum等于100.(在Javascript中)
注1:返回的数字应介于0到100之间
注2:这只是正态分布......不是负偏斜和正偏斜
function distNormal() {
var fldCnt = 14;
var _val = 100
//fldCntNotGray = $(_elm).not('.grayField').length;
//fldCntGray = fldCnt-fldCntNotGray;
//if (fldCntNotGray != 0) fldCnt = fldCntNotGray;
if (Math.abs(fldCnt)%2 === 0) {isOdd = false;} else {isOdd = true;}
if (isOdd) {
meadian = parseInt(fldCnt/2)+1;
} else {
meadian = parseInt(fldCnt/2);
}
var sum = 0;
var sumGray = 0;
var cntGray = 0;
var PI = Math.PI;
var normalPattern = '', plus, MaxMinMax, MinMaxMin, normalDistr, A, Amax, Amin, Smax, Smin, Mu, Sigma, xi, arrPattern;
normRatio = parseFloat((_val/meadian)/(fldCnt-meadian)*1.0874);
for (var i = 0; i < fldCnt; i++){
if (i < meadian) {plus = 0;} else {plus = 1;}
if (normalPattern !== '') {normalPattern += '|';}
MaxMinMax = Math.abs(parseInt((fldCnt/2-i)*-1)+plus);
MinMaxMin = Math.abs((MaxMinMax+Math.abs(MaxMinMax)-MaxMinMax)-meadian);
normalDistr = toPrecision(MinMaxMin*normRatio,1);
A = fldCnt;
Amax = _val;
Amin = 1;
Smax = 15;
Smin = 1;
Mu = A/2;
Sigma = Smax+((Smax-Smin)/(Amax-Amin))*(A-Amax);
xi = 1/(Sigma*(2*PI)^0.5) * Math.exp(-((i-Mu)^2) / (2*Sigma^2));
//normalDistr = Math.round(xi-A);
//$("input#"+_fld+"-"+(i+1)).not('.grayField').val(normalDistr);
normalPattern += normalDistr;
sum += parseFloat(normalDistr);
}
arrPattern = (normalPattern);
alert(arrPattern);
}
function toPrecision(number,precision){
var str = number.toString();
var pos = str.indexOf(".");
if(pos!=-1){
var tempArr = str.split(".");
if(tempArr[1].length<precision){
tempArr[1] = parseInt(tempArr[1])*Math.pow(10,precision-parseInt(tempArr[1]))+"";
} else{
tempArr[1] = tempArr[1].slice(0,precision);
}
str = tempArr.join(".");
} else {
var tempSuffix = "";
while(precision){
tempSuffix += "0";
precision--;
}
str += "."+tempSuffix;
}
return parseFloat(str);
}
注意:我返回的数字总和不是100 ....而且分发不准确!