3 views (last 30 days)

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

Bruno Luong
on 19 Oct 2019

Edited: Bruno Luong
on 19 Oct 2019

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)

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## Direct link to this comment

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

⋮## Direct link to this comment

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

## Direct link to this comment

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

⋮## Direct link to this comment

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

## Direct link to this comment

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

⋮## Direct link to this comment

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

Sign in to comment.