[Problem description]:If you are interested in the division of random intervals, consider the following:
Divide the Len interval into N segments, and the length of each segment is 0 ~ A random value of M. (Len, n, m is the given value, and N * m> Len ).
Or it can be described as follows:
Generate a series to meet the following requirements (Len, n, m is the given value, and N * m> Len ):
1. There are n columns;
2. Each item of the series is 0 ~ A random value of M;
3. The sum of the items in this series is Len; % %
% MATLAB Solution
% I use a MATLAB program, please comment
% The range with the length of Len is randomly divided into N blocks, and the maximum block cannot exceed M (n × m> Len)
% Algorithm Description:
% 1. First, the first n-1 items A1, A2, A3,..., A (n-1) are generated. each item is 0 ~ Random Number of m;
% 2. Confirm the last item below:
% (1) makes last = len-sum (n-1); sum (n-1) indicates the sum of the first n-1 items
% (2) Test the last value;
% (3) if the last value is equal to: Last> 0 and last <m, last is the last item and the program ends;
% (4) if last <0, an item is randomly selected from the first n-1 item, which is assumed to be the K item,
% Retrieve 0 ~ The random number S of the AK, so that last = last + S,
% (5) if last> m, an AK is randomly selected from the first n-1 items, and 0 ~ is retrieved from the last ~ (M-ak) random number S,
% Last = last-S;
% (6) repeats step (2;
Function f = rand_seq (Len, n, m)
F = m * rand (1, N) % generates a random matrix of 1 row and n columns, each of which cannot exceed M
S = len-(sum (f)-f (n); % last remaining s
Count = 0;
Index = 0;
R = 0;
While S-M> 1e-18 | S <1e-18
While S <1e-18
Index = int16 (Rand * (n-1) + 1 );
T = f (INDEX); % obtains the value of item I.
R = rand;
F (INDEX) = T * r; % extract T * (1-r) from item I and add it to S.
S = S + T * (1-r );
Count = count + 1;
End
While S-M> 1e-18
Index = int16 (Rand * (n-1) + 1 );
T = f (INDEX); % obtains the value of item I.
R = rand;
F (INDEX) = T + (m-t) * R; % add (m-t) * R to item I
S = S-(m-t) * R;
Count = count + 1;
End
End
F (n) = s; % the last item is determined
Count % Number of output cycles