Asked by waqas muhammad
on 19 Oct 2019

Hello everyone.

I need to choose a random number for each probablitiy. The code is in the attachment. The code is perfect, i just need a random number for each probability.

Its really confusing for me. So if somebody can help me to solve ths problem, i will be very thankfull..

Thanks

Answer by Bruno Luong
on 19 Oct 2019

Edited by Bruno Luong
on 19 Oct 2019

Accepted Answer

v = [ 0, 15, 25, 35, 45, 20]; % values

P = [0.01, 0.15, 0.20, 0.50, 0.12, 0.02]; % the correspond probability

c = cumsum([0 P]);

c = c / c(end); % make sure the cumulative sum terminates exactly with 1

n = 100; % length of the random sequence

[~,i] = histc(rand(1,n),c);

r = v(i)

waqas muhammad
on 21 Oct 2019

bundle of thanks Sir.

Your comment really helped me alot. i got idea from your comment and it was worked perfect.

Once again Thanks alot

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## Daniel M (view profile)

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/486234-how-can-i-generate-the-random-numbers-for-specific-probabilites#comment_758052

## waqas muhammad (view profile)

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/486234-how-can-i-generate-the-random-numbers-for-specific-probabilites#comment_758059

## Daniel M (view profile)

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/486234-how-can-i-generate-the-random-numbers-for-specific-probabilites#comment_758066

Sign in to comment.