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