probabilistic
Class Choice
java.lang.Object
probabilistic.Choice
public class Choice
- extends Object
The class Choice
contains the static method make
.
This method, given an array p
of probabilities, returns an
integer i
, where 0 ≤ i < p.length
, with
probability p[i]
. This method can be exploited to implement
randomize algorithms. For example, the following snippet mimicks a coin
flip.
double[] p = { 0.5, 0.5 };
switch (Choice.make(p))
{
case 0 : System.out.println("heads"); break;
case 1 : System.out.println("tails"); break;
}
- Author:
- Xin Zhang, Franck van Breugel
Method Summary |
static int |
make(double[] p)
Given an array p of probabilities, returns i
with probability p[i] , where 0 ≤ i < p.length . |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
make
public static int make(double[] p)
- Given an array
p
of probabilities, returns i
with probability p[i]
, where 0 ≤ i < p.length
.
- Parameters:
p
- probabilities.- Precondition:
p[0] + ... + p[p.length - 1] = 1.0
.
- Returns:
i
with probability p[i]
.