# Generate random weighted variables

In this post I describe the common problem how to generate random weighted variables. For example you have a die with 6 faces that is fixed, so the probability of an 1 is not the same with the probability of a 2 etc.

The table below shows an example:

 Face Probability 1 Β 25 2 Β 25 3 Β 20 4 Β 20 5 Β 5 6 Β 5

One easy solution is to reduce the problem to another one with a die with 100 faces that is not fixed.

You generate a random value from 0-99 that follows the uniform distribution. Do not use the modulo function to do that. See this post instead.

Once you have that value you perform the following steps:

You check the space within which that value relies.

1. between 0 and 24, you generate 1 as the weighted variable
2. between 25 and 49, you generate 2 as the weighted variable
3. between 50 and 69, you generate 3 as the weighted variable
4. between 70 and 89, you generate 4 as the weighted variable
5. between 90 and 94, you generate 5 as the weighted variable
6. between 95 and 99, you generate 6 as the weighted variable