Code covered by the BSD License

Generation of Random Variates

James Huntley (view profile)

generates random variates from over 870 univariate distributions

binomial_coef(n, k)
```% BINOMIAL_COEF computes the Binomial coefficient C(N,K).
%
%
%  Discussion:
%
%    The value is calculated in such a way as to avoid overflow and
%    roundoff.  The calculation is done in integer arithmetic.
%
%  Formula:
%
%    CNK = C(N,K) = N! / ( K! * (N-K)! )
%
%  Reference:
%
%    M L Wolfson and H V Wright,
%    Combinatorial of M Things Taken N at a Time,
%    ACM algorithm 160,
%    Communications of the ACM,
%    April, 1963.
%
%  Modified:
%
%    17 January 1999
%
%  Author:
%
%    John Burkardt
%        modified by J. Huntley, 02/19/07, to handle non-integer inputs.
%
%  Translated to Matlab:
%
%    Jim Huntley, 03/10/04, 02/19/07.
%
%  Parameters:
%
%    Input, N, K, are the values of N and K.
%
%    Output, CNK, the number of combinations of N
%    things taken K at a time.
%

function [cnk] = binomial_coef(n, k)

mn = min(k,n-k);
if (mn < 0)
cnk = 0;
elseif (mn == 0)
cnk = 1;
%elseif((fix(n) == n) & (fix(k) == k))    % Integer Inputs.
%mx = max(k,n-k);
%cnk = mx + 1;
%for i = 2:mn
%  cnk = fix((cnk .* (mx + i))./ i);
%end
else                                      % Non-Integer Input(s)
%cnk = gamma(n+1) ./ (gamma(k+1) .* gamma(n-k+1));
cnk = exp(gammaln(n+1) - gammaln(k+1) - gammaln(n-k+1));
end

return
```