%D \module
%D   [       file=supp-ran,
%D        version=1998.01.21,
%D          title=\CONTEXT\ Support Macros,
%D       subtitle=Random Number Generation,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA / Hans Hagen \& Ton Otten}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\writestatus{loading}{Third Party Macros / Random Number Generation}

%D \macros
%D   {getrandomcount, getrandomdimen,
%D    getrandomfloat, getrandomnumber,
%D    setrandomseed, getrandomseed}
%D
%D This module load Donald Arseneau's generic file
%D \type{random.tex}. A small shell is needed because we
%D redefine some \TEX\ primitives. We also use different names
%D for the two generators and add an extra one.
%D
%D \starttyping
%D \getrandomcount  \countregister {minimum} {maximum}
%D \getrandomdimen  \dimenregister {minimum} {maximum}
%D \getrandomnumber \macroname     {minimum} {maximum}
%D \getrandomfloat  \macroname     {minimum} {maximum}
%D \stoptyping
%D
%D Of course the file \type{random.tex} needs to be present.
%D To prevent name clashes, the \CONTEXT\ distribution
%D contains a copy in \type {thrd-ran.tex}.
%D
%D The randomseed can be set by:
%D
%D \starttyping
%d \setrandomseed{number>0}
%D \stoptyping
%D
%D and get by:
%D
%D \starttyping
%D \getrandomseed\randomseed
%D \stoptyping

\ifx\nextrandom\undefined

  \readfile{random.tex}
    {\writestatus{loading}{Donald Arseneau's 'random.tex'     (found)}}
    {\writestatus{loading}{Donald Arseneau's 'random.tex' (not found)}}

  % avoid scratch dimens 0 and 2 (already recoded in third-ran)
  %
  % \def\setrandim#1#2#3% dimen register, minimum length, maximum length
  %   {\scratchdimen#2\edef\!!stringa{\number\scratchdimen}%
  %    \scratchdimen#3\edef\!!stringb{\number\scratchdimen}%
  %    \setrannum\ranval\!!stringa\!!stringb
  %    #1\ranval\s!sp\relax}

\fi

\ifx\nextrandom\undefined

  \writestatus{loading}{using fake randomizer}

  \newcount\randomi

  \def\setrandim#1#2#3{\scratchdimen  #2\relax#1\scratchdimen  }
  \def\setrannum#1#2#3{\scratchcounter#2\relax#1\scratchcounter}

  \let\nextrandom\relax

%              \def\getrandomdimen #1#2#3{\scratchdimen  #2\relax#1\scratchdimen  }
%              \def\getrandomcount #1#2#3{\scratchcounter#2\relax#1\scratchcounter}
%              \def\getrandomnumber#1#2#3{\edef#1{0}}
%              \def\getrandomfloat #1#2#3{\edef#1{0}}
%   \unexpanded\def\setrandomseed      #1{}
%   \unexpanded\def\getrandomseed      #1{\edef#1{0}}

%   \chardef\randomseedfrozen\zerocount

%   \def\freezerandomseed {\global\chardef\randomseedfrozen\plusone  }
%   \def\defrostrandomseed{\global\chardef\randomseedfrozen\zerocount}

\fi

\ifx\nextrandom\undefined \endinput \fi

\ifx\normaluniformdeviate\undefined

  \let\verynormalnextrandom\nextrandom

  \def\normalnextrandom
    {\bgroup
     \let\time \normaltime
     \let\day  \normalday
     \let\month\normalmonth
     \let\year \normalyear
     \verynormalnextrandom
     \egroup}

\else

  % Yet untested.

  \writestatus{loading}{using tex's built in randomizer (overloading macro)}

  % For the meaning of the magic number, see \type {thrd-ran.tex}.

  % \def\normalnextrandom
  %   {\setrandomseed\randomi
  %    \global\randomi\normaluniformdeviate2147483647\relax}

  % Taco suggested to use the following alternative because \type
  % {\normaluniformdeviate} can return a zero (as expected) while
  % Donalds's alternative has a minimum of~1.

  \beginTEX
    \def\nextrandom
      {\normalsetrandomseed\randomi
       \global\randomi\normaluniformdeviate2147483646%
       \global\advance\randomi\plusone}
  \endTEX

  \beginETEX \numexpr
    \def\nextrandom
      {\normalsetrandomseed\randomi
       \global\randomi\numexpr\normaluniformdeviate2147483646+1\relax}
  \endETEX

  \let\normalnextrandom\nextrandom

\fi

\def\nextrandom
  {\bgroup
   \normalnextrandom
   \gdef\nextrandom{\ifcase\randomseedfrozen\normalnextrandom\fi}%
   \egroup}

\chardef\randomseedfrozen\zerocount

\def\freezerandomseed
  {\ifcase\randomseedfrozen
     \nextrandom \global\chardef\randomseedfrozen\plusone
   \fi}

\def\defrostrandomseed
  {\ifcase\randomseedfrozen\else
     \global\chardef\randomseedfrozen\zerocount \nextrandom
   \fi}

\let\getrandomcount\setrannum
\let\getrandomdimen\setrandim

\def\getrandomnumber#1#2#3%
  {\getrandomcount\scratchcounter{#2}{#3}%
   \edef#1{\the\scratchcounter}}

\def\getrandomfloat#1#2#3%
  {\getrandomdimen\scratchdimen{#2\points}{#3\points}%
   \edef#1{\withoutpt\the\scratchdimen}}

\unexpanded \def\setrandomseed#1%
  {\randomi#1\relax}

\unexpanded \def\getrandomseed#1%
  {\edef#1{\number\randomi}}

\endinput
