# How to get only random samples which are in allowed range and reject the rest?

5 views (last 30 days)
Malik Suhaib Aijaz on 10 May 2022
Answered: KSSV on 10 May 2022
Hi!
u = rand(3,1)-0.5;
y = - 10 * sign(u).* log(1- 2* abs(u));
The output y is a 3*1 column vector (of samples drawn from Laplacian Distribution).I need to reject those y values which are outside the allowed range say [-15,15](To get samples from Truncated Laplace Distribution) and only accept the in-range values.If I don't get all 3 in-range values in the first iteration,i have to go for second iteration and so on until i get 3 in-range values.
Can you please help me out with the matlab script? It would be also helpful if someone can suggest any other alternative method of drawing i.i.d samples from Truncated Laplace Distribution.
Thanks

KSSV on 10 May 2022
idx = [0 0 0] ;
while nnz(idx)~=3
u = rand(3,1)-0.5;
y = - 10 * sign(u).* log(1- 2* abs(u));
idx = y >=-15 & y <= 15 ;
end
y
y = 3×1
-3.3030 -7.1208 2.4590

R2022a

### Community Treasure Hunt

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

Start Hunting!