Code covered by the BSD License

# Generation of Random Variates

### James Huntley (view profile)

generates random variates from over 870 univariate distributions

partition(n);
```% partition.m - calling program to compute integer partitions of n.
%
% Created by:  J. Huntley,  9/26/05.
%
% Calls:  'i_partition_next2.m' - (Burkhardt) computes partions of n one at a time.
%
% Called by:  'crommelin_cdf.m', 'crommelin_pdf.m', & MANY others.
%
% Inputs:   n - integer to be partitioned.
%
% Outputs:  L - cell array of partition sets
%           cellmax - number of sets in the partition
%           a_new_hold - cell array of integers in each set
%           mult_new_hold - cell array of multiplicity of integers in each set
%

function [L, callmax, a_new_hold, mult_new_hold] = partition(n);

parts = [1 2 3 5 7 11 15 22 30 42 56 77 101 135 176 231 297 385 490 627 792 1002 ...
1255 1575 1958 2436 3010 3718 4565 5604 6842 8349 10143 12310 14883 17977 ...
21637 26015 31185 37338 44583 53174 63261 75175 999999];
more = false;
nmax = 45;
callmax = parts(min(n,nmax));
npart = 0;
a = 0;
mult = 0;

for jc = 1:callmax

[ npart_new, a_new, mult_new, more_new ] = i_partition_next2 ( n, npart, a, ...
mult, more );

npart = npart_new;
a = a_new;
mult = mult_new;
more = true;

startindx = 1;
stopindx = 0;
for js = 1:npart
stopindx = stopindx + mult_new(js);
term(startindx:stopindx) = a_new(js);
startindx = stopindx + 1;
end

L{jc} = [term(1:stopindx)];
a_new_hold{jc} = a_new;
mult_new_hold{jc} = mult_new;
clear term
if (more_new <= 0)
break
end

end

if(callmax == nmax)
callmax = jc - 1;
end

return
```