【发布时间】:2014-02-01 05:24:13
【问题描述】:
我有一部分代码生成一个随机数,然后尝试找出数字落在哪里,介于零和某个概率之间,以百分比形式存储在数组中。我知道这听起来可能比实际上更令人困惑,所以这里是我的代码,基本上是用 Java 编写的:
Random rand = new Random();
double currentProbability = rand.nextDouble();
// these are the "percentages" I refer to above
// note that they will not necessarily be in least-to-greatest/greatest-to-least
// order
double[] probabilities = new double[]{0.49, 0.49, 0.02};
// the objects in the array below correspond to the probabilities at the same
// index in the "probabilities" array above
Object[] correspondingObjects = new Object[]{new Object(), new Object(), new Object()};
// here, I would find between which percentage the random number lies, and choose
// the corresponding object from the array of Objects
因此,我的问题主要是如何选择随机数所在的索引,如果概率以百分比形式给出。也许我过于复杂了,我会要求任何认为是这种情况的用户在下面留下评论,而不是对这个问题投反对票。
【问题讨论】:
-
你想找到分布的数字范围是多少?
-
哦,对不起,这很重要:概率数组中的值总和为 100%;但是,此数组中可能只有两个概率,其余值已设置为
null
标签: java random probability