function pdf = deranged_pdf ( x, a )

%*****************************************************************************80
%
%% deranged_pdf() evaluates the Deranged PDF.
%
%  Discussion:
%
%    PDF(X)(A) is the probability that exactly X items will occur in
%    their proper place after a random permutation of A items.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    11 January 2021
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer X, the number of items in their correct places.
%    0 <= X <= A.
%
%    integer A, the total number of items.
%    1 <= A.
%
%  Output:
%
%    real PDF, the value of the PDF.
%
  if ( x < 0 | a < x )
    pdf = 0.0;
  else
    cnk = nchoosek ( a, x );
    dnmk = deranged_enum ( a - x );
    nfact = factorial ( a );
    pdf = cnk * dnmk / nfact;
  end

  return
end
