如果您的发行版可能会发生变化,那么您不希望以Kevin的答案方式对您的发行版进行硬编码,您可以使用
NavigableMap方法使用
NavigableMap.这允许您为选择选项指定权重.然后,您可以生成从0到权重总和的随机数.然后根据您的加权分布统计输出.
创建地图后,选择条目的代码非常短:
Random rand = new Random();
...
double rnd = rand.nextDouble() * totalWeight;
int elem = map.ceilingEntry(rnd).getValue();
要创建任意分布,您可以执行以下操作:
int[] options = new int[]{3,4,5,6,7,8,9,10};
double[] weights = new double[]{ 0.85/3d, 0.85/3d, 0.85/3d,
0.10/3d, 0.10/3d, 0.10/3d,
0.05/2d, 0.05/2d };
NavigableMap map = new TreeMap();
double totalWeight = 0d;
for (int i = 0; i < weights.length; i++) {
totalWeight += weights[i];
map.put(totalWeight, options[i]);
}
测试这10000多个选项给出了以下权重:
03: 28.99%
04: 28.10%
05: 28.06%
06: 3.27%
07: 3.62%
08: 3.08%
09: 2.40%
10: 2.48%
测试代码:
// select from the weighted elements
Random rand = new Random();
HashMap freqs = new HashMap();
int iterations = 10000;
for(int i = 0; i < iterations; i++) {
double rnd = rand.nextDouble() * totalWeight;
int elem = map.ceilingEntry(rnd).getValue();
freqs.put(elem, (freqs.containsKey(elem) ? freqs.get(elem) : 0) + (1d/iterations));
}
Map sortedFreqs = new TreeMap(freqs);
for(Map.Entry entry : sortedFreqs.entrySet()) {
System.out.printf("%02d: %.2f%% %n", entry.getKey(), entry.getValue() * 100d);
}