summaryrefslogtreecommitdiff
path: root/tex/context/base/mkii/supp-ran.mkii
blob: 0a39026bfd30f2057a386a6ac62c3033ed0d0767 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
%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 ADE \& \CONTEXT\ Development Team}]
%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}{ConTeXt Support 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

\input thrd-ran.mkii

\ifx\uniformdeviate\undefined

    \let\verynormalnextrandom\nextrandom

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

\else

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

    % For the meaning of the magic number, see \type {thrd-ran.tex}.
    %
    % Taco suggested to use the following alternative because \type
    % {\uniformdeviate} can return a zero (as expected) while
    % Donalds Arseneau's alternative has a minimum of~1.

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

    \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%
  {\global\randomi#1\relax} % global added

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

\def\getnewrandomseed#1%
  {\setrandomseed\minusone  % signals thrd-ran to auto reseed
   \nextrandom              % this signal is needed for the
   #1\randomi}              % pseudo randomizer (see: third-ran)

\endinput