Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random number with Probabilities

I am wondering what would be the best way (e.g. in Java) to generate random numbers within a particular range where each number has a certain probability to occur or not?

e.g.

Generate random integers from within [1;3] with the following probabilities:

P(1) = 0.2
P(2) = 0.3
P(3) = 0.5


Right now I am considering the approach to generate a random integer within [0;100] and do the following:

If it is within [0;20] --> I got my random number 1.
If it is within [21;50] --> I got my random number 2.
If it is within [51;100] --> I got my random number 3.

What would you say?

like image 278
marc wellman Avatar asked Dec 02 '13 12:12

marc wellman


People also ask

How do you generate random numbers with probability?

Generate random value with probability Select a blank cell which you will place the random value at, type this formula =INDEX(A$2:A$8,COUNTIF(C$2:C$8,"<="&RAND())+1), press Enter key. And press F9 key to refresh the value as you need.

What does random mean in probability?

Random event/process/variable: an event/process that is not and cannot be made exact and, consequently, whose outcome cannot be predicted, e.g., the sum of the numbers on two rolled dice.


1 Answers

Yours is a pretty good way already and works well with any range.

Just thinking: another possibility is to get rid of the fractions by multiplying with a constant multiplier, and then build an array with the size of this multiplier. Multiplying by 10 you get

P(1) = 2 P(2) = 3 P(3) = 5 

Then you create an array with the inverse values -- '1' goes into elements 1 and 2, '2' into 3 to 6, and so on:

P = (1,1, 2,2,2, 3,3,3,3,3);

and then you can pick a random element from this array instead.


(Add.) Using the probabilities from the example in kiruwka's comment:

int[] numsToGenerate           = new int[]    { 1,   2,    3,   4,    5   }; double[] discreteProbabilities = new double[] { 0.1, 0.25, 0.3, 0.25, 0.1 }; 

the smallest multiplier that leads to all-integers is 20, which gives you

2, 5, 6, 5, 2 

and so the length of numsToGenerate would be 20, with the following values:

1 1 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 5 5 

The distribution is exactly the same: the chance of '1', for example, is now 2 out of 20 -- still 0.1.

This is based on your original probabilities all adding up to 1. If they do not, multiply the total by this same factor (which is then going to be your array length as well).

like image 52
Jongware Avatar answered Sep 29 '22 04:09

Jongware