%%
%% This is file `lcg.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% lcg.dtx  (with options: `lcg')
%% 
%% Copyright (c) 2001, 2003, 2008 Erich Janka -- Das Vollpreiskind (janka@utanet.at)
%%
%% This package may be distributed  and/or modified under the terms of the
%% LaTeX Project Public License, as described in lppl.txt in the base
%% LaTeX distribution, either version 1.2 or (at your option)
%% any later version.
%% The latest version of this license is in
%%    http://www.latex-project.org/lppl.txt
%%
%% This program consits of the files lcg.dtx and lcg.ins
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{lcg}[2008/09/10 v1.2 generating random numbers]
\RequirePackage{keyval}
\@ifundefined{rand}{}
       {\PackageWarning{lcg}{Command `rand' already defined}}
\@ifundefined{r@ndcountername}{}
       {\PackageWarning{lcg}{Command `r@ndcountername'
         already defined}}
\@ifundefined{r@nd}{}
       {\PackageWarning{lcg}{Command `r@nd' already defined}}
\@ifundefined{initr@nd}{}
       {\PackageWarning{lcg}{Command `initr@nd' already defined}}
\@ifundefined{cutr@nger@nd}{}
       {\PackageWarning{lcg}{Command `cutr@nger@nd' already defined}}
\@ifundefined{@rderr@nd}{}
       {\PackageWarning{lcg}{Command `@rderr@nd' already defined}}
\@ifundefined{ProcessOptionsWithKVr@nd}{}
       {\PackageWarning{lcg}{Command `ProcessOptionsWithKVr@nd'
         already defined}}%
\@ifundefined{qui@t}{}
       {\PackageWarning{lcg}{Command `qui@t' already defined}}
\@ifundefined{firstletterr@nd}{}
       {\PackageWarning{lcg}{Command `firstletterr@nd' already defined}}
       %    \end{macrocode}
\@ifundefined{c@f@rst}{}
       {\PackageWarning{lcg}{Counter `f@rst' already defined}}
\@ifundefined{c@l@st}{}
       {\PackageWarning{lcg}{Counter `l@st' already defined}}
\@ifundefined{c@cr@nd}{}
       {\PackageWarning{lcg}{Counter `cr@nd' already defined}}
\@ifundefined{f@rst}{}
       {\PackageWarning{lcg}{Existing command `f@rst' conflicts
         with counter `f@rst'}}
\@ifundefined{l@st}{}
       {\PackageWarning{lcg}{Existing command `l@st' conflicts
         with counter `l@st'}}
\@ifundefined{cr@nd}{}
       {\PackageWarning{lcg}{Existing command `cr@nd' conflicts
         with counter `cr@nd'}}
\def\initr@nd{%
   \def\r@ndcountername{rand}%
   \newcount \f@rst
   \newcount \l@st
   \newcount \cr@nd
   \pr@keysr@nd%
   \ProcessOptionsWithKVr@nd{Init}%
   \p@stkeysr@nd%
   \@utputr@nd%
}  % end of \def\initr@nd
\def\reinitrand{\@ifnextchar[\@reinitr@nd{\@reinitr@nd[]}}%
\def\@reinitr@nd[#1]{%
    \pr@keysr@nd%
    \setkeys{Init}{#1}%
    \p@stkeysr@nd%
    \@utputr@nd%
}%    end of \def\reinitrand
\def\chgrand{\@ifnextchar[\@chgr@nd{\@chgr@nd[]}}
\def\@chgr@nd[#1]{%
    \@tempcnta = \z@
    \@tempcntb = \z@
    \setkeys{Init}{#1}%
    \p@stkeysr@nd%
    \@utputr@nd%
}  % end of \def\chgrand
\def\ProcessOptionsWithKVr@nd#1{%
  \let\@tempa\@empty
  \@for\CurrentOption:=\@classoptionslist\do{%
    \@ifundefined{KV@#1@\CurrentOption}%
    {}{\edef\@tempa{\@tempa,\CurrentOption,}}}
  \edef\@tempa{%
    \noexpand\setkeys{#1}{\@tempa\@ptionlist{\@currname.\@currext}}}
  \@tempa
  \AtEndOfPackage{\let\@unprocessedoptions\relax}}
\define@key{Init}{first}[1]{\f@rst = #1}
\define@key{Init}{last}[2147483647]{\l@st = #1}
\define@key{Init}{counter}[rand]{\def\r@ndcountername{#1}}
\define@key{Init}{seed}[\z@]{% seed for random number generator
   \ifnum #1 < \z@%
      \PackageWarning{lcg}{Seed should be > 0 --
              Seed will be initialized with the actual time}%
      \cr@nd = \z@%
   \else%
      \cr@nd = #1
      \typeout{Random number generator initialized to #1}%
   \fi%
}
\define@key{Init}{quiet}[y]{
   \def\qui@t{\expandafter\firstletterr@nd #1\delimiter}
   \if \qui@t y% nothing to do
   \else\if\qui@t Y \def\qui@t{y}
   \else\if\qui@t j \def\qui@t{y}
   \else\if\qui@t J \def\qui@t{y}
   \else\if\qui@t n \def\qui@t{n}
   \else\if\qui@t N \def\qui@t{n}
   \else
      \PackageWarning{lcg}{Value of key <quiet> must be <y> or <n>}
      \def\qui@t{y}
   \fi\fi\fi\fi\fi\fi
}
\def\pr@keysr@nd{%
    \f@rst = \@ne        % 1
    \l@st = 2147483647   % 2147483647
    \cr@nd  = \z@        % 0
    \@tempcnta = \z@
    \@tempcntb = \z@
    \def\r@ndcountername{rand}%
    \def\qui@t{n}
} % end of newcommand\def\pr@keysr@nd
\def\p@stkeysr@nd{%
   \@rderr@nd%                  last < first  -> swap
   \cutr@nger@nd%               range too big -> cut
   \@ifundefined{c@\r@ndcountername}{\newcounter{\r@ndcountername}}%
   {%
      \PackageWarning{lcg}{Using an already existing
           counter \r@ndcountername}%
   }%
\setcounter{\r@ndcountername}{0}%
} % end of \def\p@stkeysr@nd
\def\firstletterr@nd#1#2\delimiter{#1}
\def\@utputr@nd{%
   \if \qui@t y% do nothing
   \else
     \typeout{Smallest possible random number: \the\f@rst}%
     \typeout{Largest possible random number:  \the\l@st}%
     \typeout{The pseudo random numbers will be stored
      in the LaTeX counter `\r@ndcountername'}%
   \fi
}
\def\@rderr@nd{%
   \ifnum \l@st < \f@rst%
       \PackageWarning{lcg}{Key `last' less than key `first'
               -- swapped}%
       \@tempcnta = \f@rst
       \f@rst = \l@st
       \l@st = \@tempcnta
   \fi%
}%   end of \def\@rderr@nd
\def\cutr@nger@nd{%
    \ifnum\l@st<\z@\else
    \@tempcntb =  -2147483646   % -2^31 + 2
    \@tempcnta = \f@rst
    \advance \@tempcntb \l@st
    \multiply \@tempcntb \m@ne
    \advance \@tempcnta \@tempcntb
    \ifnum \@tempcnta < \z@%
       \PackageWarning{lcg}{Range contains too many numbers
                     -- right border reset to largest possible value}%
       \advance \l@st \@tempcnta
    \fi%
    \fi%
}%   end of \checkr@ange
\def\rand{%
   \r@nd%
   \@tempcnta
   \@tempcntb
   \@tempcnta = \f@rst
   \@tempcntb = \l@st
   \multiply \@tempcnta \m@ne
   \advance \@tempcntb \@tempcnta
   \advance \@tempcntb \@ne         %l@st-f@rst+1
   \@tempcnta = 2147483647
   \divide \@tempcnta \@tempcntb
   \multiply \@tempcnta \@tempcntb
   \ifnum \cr@nd > \@tempcnta
      \rand%
   \else
      \setcounter{\r@ndcountername}{\cr@nd}%
      \@tempcnta = \cr@nd
      \divide \@tempcnta \@tempcntb
      \multiply \@tempcnta \@tempcntb
      \multiply \@tempcnta \m@ne
      \addtocounter{\r@ndcountername}{\@tempcnta}%
      \addtocounter{\r@ndcountername}{\f@rst}%
   \fi
} % end of \rand
\def\r@nd{%
   \ifnum \cr@nd < \@ne%       then  ... initialize generator
      \cr@nd = \the\time
      \advance \cr@nd  \inputlineno
      \multiply \cr@nd  \thepage
      \advance \cr@nd \the\year
      \multiply \cr@nd \the\month
      \multiply \cr@nd \the\day
      \advance \cr@nd \inputlineno
      \if \qui@t y%
      \else
         \typeout{Random number generator initialized to \the\cr@nd}%
      \fi
      \r@nd%
   \else                         %  else ... generate new number
      \@tempcnta = \cr@nd
      \divide \@tempcnta  127773    % \@tempcnta = floor(z/q)
      \@tempcntb = \@tempcnta       % \@tempcntb = floor(z/q)
      \multiply \@tempcnta  -2836   % \@tempcnta = -r*floor(z/q)
      \multiply \@tempcntb -127773  % \@tempcntb = -q*floor(z/q)
      \advance \cr@nd \@tempcntb    % cr@nd = z mod q
      \multiply \cr@nd 16807        % cr@nd = a * (z mod q)
      \advance \cr@nd \@tempcnta    % cr@nd = a*z mod m
      \ifnum \cr@nd < \z@%
         \advance \cr@nd 2147483647 % cr@nd = (a*z mod m) > 0
      \fi
   \fi
}%        end of \r@nd
\initr@nd      % initialize the package
\endinput
%%
%% End of file `lcg.sty'.