我有以下问题:
我需要从列表
中选择一个随机对象。如果所有元素都有相同的机会被选取,这很简单。
就我而言,对象被拾取的机会存储在另一个List
中。所以我需要一种方法,根据另一个 List
从列表中随机选择一个元素。
编辑: 例如
List<String> objects = Arrays.asList("one","two","three");
List<Double> chance = Arrays.asList(0.25, 0.25, 0.5);
现在我想要String
“one
”和“two
”(概率为四分之一)和String
“三
”,概率为二分之一。
感谢您的建议。
最佳答案
你可以 TreeMap
以之前概率的当前总和作为键,以对应对象作为值,然后生成 0
和 1
之间的随机数,最后使用 ceilingEntry(K key)
获取第一个大于或等于当前随机值的key对应的对象。
类似于:
List<String> objects = Arrays.asList("one","two","three");
List<Double> chance = Arrays.asList(0.25, 0.25, 0.5);
// Build the tree map
TreeMap<Double, String> map = new TreeMap<>();
double total = 0.0d;
for (int i = 0; i < objects.size(); i++) {
map.put(total += chance.get(i), objects.get(i));
}
System.out.printf("The generated is map %s%n", map);
// The generator of random numbers
Random generator = new Random();
// Generate a random value between 0 and 1
double value = generator.nextDouble();
// Get the object that matches with the generated number
String object = map.ceilingEntry(value).getValue();
System.out.printf("The current value is %f corresponding to '%s'%n", value, object);
输出:
The generated map is {0.25=one, 0.5=two, 1.0=three}
The current value is 0,048460 corresponding to 'one'
所以这里:
- 如果随机值小于或等于
0.25
,我们将得到“one
”。 - 如果随机值介于
0.25
(不包括)和0.50
(包括)之间,我们将得到“two
”。 - 如果随机值介于
0.50
(不包括)和1.0
(包括)之间,我们将得到“三
”。
感谢 nextDouble()
返回一个 double
值,在 0.0
和 之间均匀分布>1.0
,这足以获得预期的分布。
关于java - 以不同的概率随机选择一个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41965243/