I want to generate a random number with a given probability but I'm not sure how to:
I need a number between 1 and 3
num = ceil(rand*3);
but I need different values to have different probabilities of generating eg.
0.5 chance of 1
0.1 chance of 2
0.4 chance of 3
I'm sure this is straightforward but I can't think of how to do it.
A slightly more general solution would be:
x
distributed as desired.The answers so far are correct, but slow for large inputs: O(m*n) where n is the number of values and m is the number of random samples. Here is a O(m*log(n)) version that takes advantage of monotonicity of the
cumsum
result and the binary search used inhistc
:The simple solution is to generate a number with a uniform distribution (using
rand
), and manipulate it a bit:or in a one-liner:
Explanation
Here
r
is a uniformly distributed random number between 0 and 1. To generate an integer number between 1 and 3, the trick is to divide the [0, 1] range into 3 segments, where the length of each segment is proportional to its corresponding probability. In your case, you would have:The probability of
r
falling within any of the segments is proportional to the probabilities you want for each number.sum(r >= cumsum([0, prob]))
is just a fancy way of mapping an integer number to one of the segments.Extension
If you're interested in creating a vector/matrix of random numbers, you can use a loop or
arrayfun
:Of course, there's also a vectorized solution, I'm just too lazy to write it.
using
randsample
function from Statistics and Machine Learning Toolbox, you can generate random numbers with specified probability mass function (pmf):I think this is the easiest method.