MATLAB Answers

Generate random numbers without repetition according to a probability distribution

25 views (last 30 days)
Salwa Mostafa
Salwa Mostafa on 8 Mar 2021
Answered: Jeff Miller on 8 Mar 2021
The function randsrc can be used to generate random numbers according to a certain probabiluity distribution but it allows repetition for the generated numbers. I want a function that can generate random numbers according to a certain probability distribution but without repetition?

Answers (1)

Jeff Miller
Jeff Miller on 8 Mar 2021
I assume you mean a discrete distribution since the probability of repetition is zero in a continuous one.
In that case, the easiest way is to set up a markov chain that disallows state repetitions. Here is a simple example for a random variable that has 3 possible values (i.e, 1,2,3). The first row of P indicates that if the current observation is 1, then the next observation is equally likely to be 2 or 3. The second row of P says if the current observation is 2, then the next one will be 1 with probability 0.6 and 3 with probability 0.4. And so on. You can adjust the size of the P matrix and the values in it to model any discrete distribution you want, and the zero's on the diagonal indicate no repetitions.
P = [ 0 0.5 0.5;
0.6 0 0.4;
0.8 0.2 0];
mc = dtmc(P);
X = simulate(mc,10)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!