File Exchange

RANDINTERVAL

version 1.0 (3.81 KB) by

random numbers within multiple intervals

Updated

R = RANDINTERVAL(N, INTERVALS) returns a N-by-N matrix R of random numbers taken from a uniform distribution over multiple intervals.

Similar to RAND, RANDINTERVAL([N M],..) returns a N-by-M matrix, and RANDINTERVAL([N M P ...],..) returns a N-by-M-by-P-by-.. array. Note that a notation similar to rand(N,M,P,..) is not available.

INTERVALS is a N-by-2 matrix in which the rows specify the intervals from which the numbers are drawn. Each interval is given by a lower bound (in the first column of INTERVALS) and an upper bound (2nd column of INTERVALS). If the lower bound equals the upper bound, no numbers will be drawn from that interval. The upper bound cannot be smaller than the lower bound.
If INTERVALS is a vector (K-by-1 or 1-by-K), the intervals are formed as (INTERVALS(1),INTERVALS(2)), (INTERVALS(2),INTERVALS(3)), ..., (INTERVALS(K-1),INTERVALS(K)). In this case, the number of intervals N is one less than the number of elements K, i.e., N=K-1. The relative length of an interval determines the likelihood that a number is drawn from that interval. For instance, when INTERVALS is [1 2 ; 10 12], the probablity that a number is drawn from the first interval (1,2) is 1 out of 3, vs 2 out of 3 for the second interval (10,12).

R = RANDINTERVAL(N, INTERVALS, WEIGHTS) allow for weighting each interval specifically. WEIGHTS is vector with N numbers. These numbers specify, in combination with the length of the corresponding interval, the relative likelihood for each interval (i.e., the weight). Larger values of W(k) increases the likelihood that a number is drawn from the k-th interval.
For instance, when INTERVALS is [11 12 ; 20 21] and WEIGHTS is [3 1], the probability that a numbers is drawn from the first interval is 3 out of 4, vs 1 out of 4 for the second interval. Note that both intervals have the same length. When the intervals do not have the same lengths, the likelihood that a number is drawn from an interval k, with length L(k) and weight W(k) is given by the formula: p(k) = (W(k) x L(k)) / sum(W(i) ./ L(i)),i=1:N

[R, IND] = RANDINTERVAL(..) also returns a index array IND, which has the same size as R. IND contains numbers between 1 and the number of intervals. Intervals with zero length and/or a weight of zero will not be present in IND.

Notes:
- overlapping intervals are allowed, but may produce hard-to-debug results. In this case a warning is issued. On the other, such overlapping intervals could be (ab-)used to draw from specific distributions.
- for a single interval (A B) this function is equivalent to A + (B-A) * rand(N)

Example of basic use without weights:
randinterval([1,10000], [1 3 ; 5 7 ; 10 15 ; 20 26]) ;
returns a row vector with 10000 numbers between 1 and 3, between 5 and 7, between 10 and 15, or between 20 and 26. Approximately 2/5 of the numbers is between 20 and 26.

More examples are given in the help section.

Version 1.0, oct 2008

John D'Errico

Expansive, readable help. A useful, well written tool that does what it claims to do.

boluor New

it's perfect.thank you!

MATLAB 7 (R14)