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
Show older comments
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
Accepted Answer
More Answers (2)
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 Comments
Stephen23
on 8 Mar 2018
Note that the first method will produces integers with different probabilities, depending on the selected ranges.
John D'Errico
on 8 Mar 2018
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
on 8 Mar 2018
N = 100 ;
A = randi([50 150],1,N) ; % Generate random numbers between 50 and 150
A(A>=110 & A<=120) = [] ; % Remove undesired numbers
1 Comment
John D'Errico
on 8 Mar 2018
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.
Categories
Find more on Random Number Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!