# 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

35 views (last 30 days)
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

Roger Stafford on 8 Mar 2018
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.
John D'Errico on 8 Mar 2018
+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.

Birdman on 8 Mar 2018
Edited: Birdman on 8 Mar 2018
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 CommentsShowHide 1 older comment
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
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.