Code covered by the BSD License

# Generation of Random Variates

### James Huntley (view profile)

generates random variates from over 870 univariate distributions

dist_nex(k,m)
```function[d,jmax] = dist_nex(k,m)
%
%*******************************************************************************
%
% DIST_NEX returns the distribution and number of indistinguishable objects.
%
%
%  Example:
%
%    K = 3, M = 5
%
%    0           0           5
%    0           1           4
%    0           2           3
%    0           3           2
%    0           4           1
%    0           5           0
%    1           0           4
%    1           1           3
%    1           2           2
%    1           3           1
%    1           4           0
%    2           0           3
%    2           1           2
%    2           2           1
%    2           3           0
%    3           0           2
%    3           1           1
%    3           2           0
%    4           0           1
%    4           1           0
%    5           0           0
%
%    jmax = 21
%
%  Reference:
%
%    Robert Fenichel,
%    ACM Algorithm 329,
%    Distribution of Indistinguishable Objects into Distinguishable Slots,
%    Communications of the ACM,
%    Volume 11, page 430, June 1968.
%
%  Modified:
%
%    26 June 1999; 30 March, 2011
%
%  Author:
%
%    John Burkardt (FORTRAN90 - single vector code); Jim Huntley (Matlab -
%       code to generate all permutation vectors)
%
%  Parameters:
%
%    Input, integer K, the number of distinguishable "slots".
%
%    Input, integer M, the number of indistinguishable objects.
%
%    Input/output, integer Q(K); for slot I, Q(I) is the number of objects
%    in that slot.
%
%    Input/output, logical MORE, used to start and stop the computation
%    [removed in code mod that generates all permutation vectors].
%
%    On first call, the user should set MORE = .FALSE, to signal the
%    routine that this is a startup.  The routine will set MORE = .TRUE.,
%    and return the first distribution.  If the user calls again, the
%    routine will try to find the next distribution.  On each call, the
%    next distribution is found, and MORE is .TRUE., but finally, when
%    there is no next distribution, MORE is returned as .FALSE.

jmax = int8(exp(gammaln(m+k)-gammaln(k)-gammaln(m+1)));     %Total Number of permutations

q(1:k-1) = 0;
q(k) = m;
leftmost = k + 1;

for jj = 1:jmax

if ( q(1) == m )
q(1:k-1) = 0;
q(k) = m;
leftmost = k + 1;

elseif ( leftmost < k+1 )
leftmost = leftmost - 1;
q(k) = q(leftmost) - 1;
q(leftmost) = 0;
q(leftmost-1) = q(leftmost-1) + 1;
if ( q(k) ~= 0 )
leftmost = k + 1;
end

else
if ( q(k) == 1 )
leftmost = k;
end
q(k) = q(k) - 1;
q(k-1) = q(k-1) + 1;

end

d(jj,:) = q;

end

return```