how can i generate the random numbers for specific probabilites
조회 수: 2 (최근 30일)
이전 댓글 표시
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
댓글 수: 3
Daniel M
2019년 10월 19일
I still don't know what you want. Your code does not make that easier. Do you want to generate random numbers between two integers? That's very easy.
N = 100; % generate N random numbers
rnge = [1 15]; % on this interval
nums = randi(rnge,1,N); % generate random integers
nums2 = rand(1,N)*14 + 1; % generate random (non-integer) numbers in rnge
채택된 답변
Bruno Luong
2019년 10월 19일
편집: Bruno Luong
2019년 10월 19일
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)
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!