Java: How do I simulate probability?

java, probability

Solution

The `Random` class allows you to create a consistent set of random numbers so that every time you run the program, the same sequence of values is generated. You can also generate normally distributed random values with the Random class. I doubt you need any of that.

For what you describe, I would just use Math.random. So, given the age of a man we could write something like:

double prob = manDeathTable[age];
if( Math.random() < prob )
   virtualManDiesThisYear();

Problem

I have a set of over 100 different probabilities ranging from 0.007379 all the way to 0.913855 (These probabilities were collected from an actuary table http://www.ssa.gov/oact/STATS/table4c6.html). In Java, how can I use these probabilities to determine whether something will happen or not? Something along these lines... ``` public boolean prob(double probability){ if (you get lucky) return true; return false; } ```

Original source