How can i generate continuous random numbers with multiple limit zones in matlab? For ex:i want numbers btw 50 and 150 but it should not contain numbers from 110 to 120
조회 수: 12 (최근 30일)
이전 댓글 표시
118104003 MPS X sem Sai Vamsi DV
2018년 3월 8일
댓글: John D'Errico
2018년 3월 8일
How can i generate continuous random numbers with multiple limit zones in matlab? For ex:i want numbers btw 50 and 150 but it should not contain numbers from 110 to 120
댓글 수: 0
채택된 답변
Roger Stafford
2018년 3월 8일
I interpret your words "continuous random numbers" to mean all real numbers within the given limits, not just integers. Suppose you want values within the intervals as given by the rows of p, that is, for example, between 50 and 110, between 120 and 150, between 208.2 and 321.9, or between 410.7 and 531.8. They should all have a constant probability density within these intervals.
n = 1000; % Get 1000 random numbers within those ranges
p = [50,110;120,150;208.2,321.9;410.7,531.8]; % As mentioned above
d = p(:,2)-p(:,1); % Widths of intervals
c = cumsum(d); % Cumulative widths
r = zeros(n,1); %Allocate space for random values
for k = 1:n
m = sum(c(1:end-1)<c(end)*rand)+1; % Random interval choice
r(k) = p(m,1)+d(m)*rand; % Random position within interval
end
The intervals are chosen with probability in proportion to their respective lengths, and position within any interval is statistically uniform. The 'r' vector should contain the desired (continuous) random values.
댓글 수: 1
John D'Errico
2018년 3월 8일
+1 of course. Roger shows how to sample uniformly across multiple disjoint intervals, proportionally to their lengths. For a large number of samples, this might be a bit inefficient, but loops are not that bad. The sampling could have been efficiently done using a vectorized form. For example, use a binning tool such as histcounts to decide which interval a sample lies in (based on the cumulative widths in c.) Given that, the generation of random samples takes essentially one more line, all fully vectorized with no loop required.
추가 답변 (2개)
Birdman
2018년 3월 8일
편집: Birdman
2018년 3월 8일
One approach:
a=randi([50 110],1,100)
b=randi([120 150],1,100)
c=horzcat(a,b)
You desired set of numbers are in c variable. Or:
a=setdiff(randi([50 150],1,200),110:120)
댓글 수: 2
Stephen23
2018년 3월 8일
Note that the first method will produces integers with different probabilities, depending on the selected ranges.
John D'Errico
2018년 3월 8일
This answer has several (easily addressed) problems. One is that it presumes random integers, not a continuous form. That is easily fixed, using rand.
The second problem is it forces the user to choose how many samples occur in each interval. As you have done here, the different intervals will be sampled unequally, thus relatively more from one interval than the other.
To make this a more valid random sampling scheme, you would want to change to a continuous distribution, then sample from each interval according to a proper rate. See that one of the intervals is longer than the other as you have it. Were the sampling scheme presumed uniform, you would want to sample from each interval proportionately to the length of that interval.
Finally, a good idea would be to randomly permute the sequence of the resulting samples at the end.
KSSV
2018년 3월 8일
N = 100 ;
A = randi([50 150],1,N) ; % Generate random numbers between 50 and 150
A(A>=110 & A<=120) = [] ; % Remove undesired numbers
댓글 수: 1
John D'Errico
2018년 3월 8일
While this is technically correct for part of the question, i.e., in the use of rejection to solve the problem, the use of randi makes it invalid, since continuous random numbers were explicitly requested. Had you used rand in an appropriate form, I would agree with your answer as fully valid. Even there, one would be making the assumption of uniform random generation over the indicated domain.
참고 항목
카테고리
Help Center 및 File Exchange에서 Random Number Generation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!