我必须从Beta Pert Distribution开始生成一些随机值,其中
我正在使用https://commons.apache.org/proper/commons-math/来计算它。
import org.apache.commons.math3.distribution.BetaDistribution;
public class BetaPertSample {
public static void main(String[] args) {
double alpha = 3;
double beta = 3;
BetaDistribution betaDistribution = new BetaDistribution(alpha, beta);
for(int i = 0; i < 10; i++){
System.out.println(Arrays.toString(betaDistribution.sample(5)));
}
}
}
这是我得到的输出:
[0.15469278774304743, 0.24006832197532496, 0.6700160564372124, 0.7894630998767309, 0.2677010833216599]
[0.36056975340251546, 0.3124278049270154, 0.8658808329431393, 0.26375527172156227, 0.8555773129311253]
[0.744334606161563, 0.46750034467932317, 0.473121312112313, 0.4080913462078363, 0.5310363769431545]
[0.6139199704205458, 0.3476524835684532, 0.8281254784392859, 0.6268730007118077, 0.8098734242274822]
[0.7636797547314598, 0.762305457843508, 0.5388498173504441, 0.27341825226924754, 0.4343036591804546]
[0.47934369329716003, 0.6531112391118911, 0.6009115030757197, 0.38518464649812034, 0.41586246696133816]
[0.3089421844688092, 0.13246689875036158, 0.7021569072987267, 0.4986960314566049, 0.3658353769573018]
[0.6536741481530508, 0.40880102770023397, 0.6428905183726568, 0.9060595485315608, 0.8191084145860973]
[0.4054538607032805, 0.7515255957117897, 0.5136013862648002, 0.5561796562706204, 0.7119459715806274]
[0.5096158228805395, 0.3400939515348804, 0.7143693299785974, 0.6251738293413021, 0.5893953597814027]
生成的值似乎具有范围[0,...,1]。
如何自定义生成值的范围,比如说,[150, 3900]?
在 Excel 中,我使用BETA.INV 函数
我可以做这样的事情来转换样本数据并在一定范围内得到结果[min,..., max],但我希望 Apache commons-math 库中有一个合适的方法。
public static void main(String[] args) {
double alpha = 3;
double beta = 3;
int min = 10;
int max = 100;
BetaDistribution betaDistribution = new BetaDistribution(alpha, beta);
for(int i = 0; i < 10; i++){
double[] sample = betaDistribution.sample(5);
double[] sampleInRange = Arrays.stream(sample)
.map(x -> {
return Precision.round(min + x * (max-min), 2);
})
.toArray();
System.out.println(Arrays.toString(sampleInRange));
}
}

