Asked by 118104003 MPS X sem Sai Vamsi DV
on 8 Mar 2018

Answer by Roger Stafford
on 8 Mar 2018

Accepted Answer

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

Sign in to comment.

Answer by Birdman
on 8 Mar 2018

Edited by 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)

Stephen Cobeldick
on 8 Mar 2018

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.

Sign in to comment.

Answer by 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

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.