Question: How to generate a random integer with customized probabilities

Hi everyone,

In the RandomTools package, the Generate(integer(range = A..B)) function generates a random integer in the range A..B. All integers in that range have the same probability to be generated, that is, 1/nops([seq(A..B)]). However, I would like to specify the probabilities of each integer. How to do so?

Example: range = 1..5. Instead of P(X=j)=1/5 with j =1,2,3,4,5, let's say the probabilities should be as follows:

P(X=1) = 0.2, P(X=2) = 0.5, P(X=3) = P(X=4) = P(X=5) = 0.1

How to generate a random integer between 1 and 5 with these probabilities?

Thank you in advance.

Please Wait...