# Selecting a random number with some probability

572 views (last 30 days)
Aftab Ahmed Khan on 25 Feb 2015
Hello Everyone, I am using this one line of code to generate a single value either to be 1 or 2 with equal probability but my question is that how can i select the value to be 1 with 60% probability and the value to be 2 with 40% probability ? Thank you.
select=randi(2,1,1);

Torsten on 25 Feb 2015
x=rand;
if x<0.6
select=1;
else
select=2;
end
Best wishes
Torsten.

#### 1 Comment

Aftab Ahmed Khan on 25 Feb 2015
Thank you all for the response. Take care.

pankaj singh on 10 Mar 2018
The simplest technique is to use inbuilt Matlab function 'randscr'.
Suppose you want to generate M by N matrix of W, X, Y, and Z with probabilities i,j,k, and l. Then use
out = randsrc(M,N,[W,X,Y,Z;i,j,k,l]); % i+j+k+l = 1;
In your case, as you want a single value to be generated, your M x N = 1 x 1 matrix; the values are 1 with 60% probability (i.e. 0.6) and 2 with 40% (i.e. 0.4) probability, therefore use this;
out = randsrc(1,1,[1,2;0.6,0.4]);
Note that the above is just an example. You can create any matrix size with any number of values. The sum of probabilities must be equal to 1.

Carles Alfageme on 1 May 2020
Hi,
I am trying to use this function: randsrc(M,N,[W,X,Y,Z;i,j,k,l]); in a Matlab Function block in Simulink.
The problem is that when i simulate my simulink model, this error message appears:
"Function 'randsrc' not supported for code generation.
Function 'MATLAB Function3' (#87.24.74), line 3, column 5: "randsrc(1,1,[0.6,0.7,0.8,0.9;0.25,0.25,0.25,0.25])" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error"
I think it is because this function comes from Communication Toolbox, but I am not sure.
Do you have any idea or suggestion?
Ana Gabriela Arteaga on 11 Jun 2020
same problem.

Daniel on 25 Feb 2015
You can use rand, which gives uniform distribution and look if the number is below or above 0.6.
if(0.6 <= rand()){
select = 1;
} else {
select = 2;
}
That should give 60/40 chances. There are more elegant ways to do that though.

Trung Khoa Le on 17 Jan 2019
Could you please give me a bit explanation why this way makes sense or some documentation that I can read to gain some intuition? Thanks
Luciano Anastassiou on 22 May 2019
Hi Trung Khoa Le,
The intuition is simply that "rand" generates a random number between 0 and 1. Then when you apply the "if" constraint, you are telling the system to only give out the result "select = 1" when that random number is below 0.6. Otherwise it will give out "select = 2".
If you repeated this 1000s of times, it would give out "select = 1" 60% of the time, because 60% of those random numbers between 0 and 1 will be below 0.6, and the other 40% of the time it will give out "select = 2".

Jos (10584) on 25 Feb 2015
Edited: Jos (10584) on 25 Feb 2015
For two values it is simple
VAL = [10 20] % 2 values
P = .8 % probabbility of selecting first value
Ndraw = 20 % number of draws
R = rand(Ndraw,1) < P
SEL = VAL(2 - R) % use as index into VAL
For more complicated cases you might be interested inTake a look at my RANDP function, which picks random values with relative probabilities.

Ka Mirul on 14 Nov 2017
I have create a video about generating random number in MATLAB