summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/lcg/lcg.sty
blob: 8b615ea0abbefa162f55e7775623b72d7cc2eced (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
%%
%% This is file `lcg.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% lcg.dtx  (with options: `lcg')
%% 
%% Copyright (c) 2001--2013 Erich Janka (erich.janka@gmail.com)
%%
%% 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}[2013/08/09 v1.3 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 \value{page}
      \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
      \global\cr@nd=\cr@nd % persist the change outside current scope
   \fi
}%        end of \r@nd
\initr@nd      % initialize the package
\endinput
%%
%% End of file `lcg.sty'.