Debian Codegraph @

texmf-dist/source/latex/lcg/lcg.dtx

raw
1 % \iffalse
2 % File: lcg.dtx
3 %% Copyright (c) 2001--2013 Erich Janka ([email protected])
4 %%
5 %% This package may be distributed and/or modified under the terms of the
6 %% LaTeX Project Public License, as described in lppl.txt in the base
7 %% LaTeX distribution, either version 1.2 or (at your option)
8 %% any later version.
9 %% The latest version of this license is in
10 %% http://www.latex-project.org/lppl.txt
11 %%
12 %% This program consits of the files lcg.dtx and lcg.ins
13 %
14 %<lcg>\NeedsTeXFormat{LaTeX2e}
15 %<lcg>\ProvidesPackage{lcg}[2013/08/09 v1.3 generating random numbers]
16 %<lcg>\RequirePackage{keyval}
17 %
18 %<*driver>
19 \documentclass{ltxdoc}
20 \usepackage[first=10, last=20]{lcg}
21 \begin{document}
22 \GetFileInfo{lcg.sty}
23 \title{The \texttt{lcg} package}
24 \author{Erich Janka\\ \texttt{[email protected]}}
25 \date{2013/08/09 (v1.3)}
26 \maketitle
27 \DocInput{lcg.dtx}
28 \end{document}
29 %
30 %</driver>
31 % \fi
32 %
33 % \iffalse
34 %<*lcg>
35 % \fi
36 %
37 % \newcommand{\mod}{\;\;\mbox{mod}\;\;}
38 % \newcommand{\key}[1]{\meta{#1}}
39 % \newcommand{\cntr}[1]{\texttt{#1}}
40 %
41 % \begin{abstract}
42 %
43 % This Package contains macros to generate (pseudo) random numbers
44 % with \LaTeX\ by a simple linear congruential generator.
45 % The user can specify a range of integers
46 % containing the generated random numbers.
47 %
48 % To pass options to the package, the \meta{key}=\meta{value}
49 % scheme of the \texttt{keyval} package is used.
50 %
51 % If no options are specified, random numbers will be
52 % generated in the interval from $1$ to $2^{31}-1 \;\;(= 2147483647)$.
53 % Let $S$ be the smallest and $L$ the largest number of the specified range,
54 % then the following inequalities must hold because of
55 % limitations of \LaTeX:
56 %
57 % $$ -2^{31}+1 \leq S \leq L \leq 2^{31}-1 \qquad\mbox{and}\qquad
58 % L-S \leq 2^{31}-2 $$
59 %
60 % The generated random numbers will be stored in a \LaTeX\
61 % counter definable by the user.
62 % \end{abstract}
63 %
64 % \section{User Interface}
65 %
66 % The \texttt{lcg} package is loaded with\\[1ex]
67 % \hspace*{2ex}\cmd{\usepackage\oarg{list of options}\{lcg\}}.\\[1ex]
68 % The optional Argument is a comma separated list of entries of
69 % the kind \key{key}=\key{value} or \key{key}. In the second case
70 % the key will be set to a standard value.
71 % For example the line\\[1ex]
72 % \hspace*{2ex}\cmd{\usepackage[first=10, last=20]\{lcg\}}\\[1ex]
73 % loads the package and generates the \LaTeX\ counter \cntr{rand}
74 % which will hold pseudo random numbers from $10$ to $20$.
75 % All available keys and their standard values are introduced below.
76 % \begin{macro}{\rand}
77 % Each call of the command \cmd{\rand} will write a new random number
78 % to the counter provided by the user with the key \key{counter} or
79 % to the standard counter of this package---\cntr{rand}. Now
80 % it's possible to do whatever can be done with counters. The command
81 % evokes a linear congruential random number generator described below.
82 % \end{macro}
83 % \begin{macro}{\reinitrand}
84 % The command \cmd{\reinitrand}\oarg{list of options} has
85 % one optional argument which is identical to the argument of
86 % \cmd{\usepackage},
87 % i.~e. the same keys in the comma separated list are allowed.
88 % The effect is that specified keys will be set and all others
89 % will be reset to their standard values.
90 % \end{macro}
91 % \begin{macro}{\chgrand}
92 % The syntax of the command \cmd{\chgrand} is identical to \cmd{\reinitrand}.
93 % The difference is that \cmd{\chgrand} will set the specified keys but
94 % won't effect any other key.
95 % \end{macro}
96 %
97 % \subsection{The Options}
98 %
99 % This section deals with the list of all available keys
100 % and their standard values.
101 % \begin{macro}{counter}
102 % This key sets the name of the \LaTeX\ counter where
103 % the random numbers will be stored.
104 % If the counter already exists (maybe somebody likes
105 % random page numbers), \LaTeX\ will prompt a
106 % warning and will use it. If the counter doesn't exist,
107 % it will be defined by this package and set to $0$.\\
108 % \textbf{Standard value:} rand
109 % \end{macro}
110 % \begin{macro}{first}
111 % This key sets the left boarder of the range within
112 % random numbers will be generated.
113 % Its value can be any number from $-2^{31}+1$ to $2^{31}-1$,
114 % as long as it is not greater than the value of the key \key{last}
115 % and the difference to the value of \key{last} doesn't exceed
116 % $2^{31}-2$.\\
117 % \textbf{Standard value:} $1$
118 % \end{macro}
119 % \begin{macro}{last}
120 % This key sets the right boarder of the range within
121 % random numbers will be generated. Its value can be any
122 % number from $-2^{31}+1$ to $2^{31}-1$,
123 % as long as it is not less than the value of the key \key{first}
124 % and the difference to the value of \key{first} doesn't exceed
125 % $2^{31}-2$.\\
126 % \textbf{Standard value:} $2^{31}-1$
127 % \end{macro}
128 % \begin{macro}{seed}
129 % The value of this key is the starting value
130 % for the algorithm generating the random numbers
131 % and must be within the range $1$ to $2^{31}-1$.
132 % If the value is smaller than $1$,
133 % the random number generator will
134 % be initialized with the time, the page number and
135 % the actual line of the file. This key allows
136 % reproduction of the sequences of random numbers.\\
137 % \textbf{Standard value:} $0$
138 % \end{macro}
139 % \begin{macro}{quiet}
140 % When using the \texttt{lcg} package it sends some lines of
141 % information to the screen and the \texttt{log}-file:
142 % \begin{itemize}
143 % \setlength{\itemsep}{-3pt}
144 % \item[--] The name of the counter holding the generated random numbers
145 % \item[--] The lower bound of the range of random numbers
146 % \item[--] The upper bound of the range of random numbers
147 % \item[--] The initial value of the random number generator
148 % \end{itemize}
149 % To supress this output this key
150 % can be used. If the value starts with the letter \emph{y}, \emph{Y},
151 % \emph{j} or \emph{J}
152 % there will be no output whereas words beginning
153 % with the letters \emph{n} or \emph{N} won't supress it.\\
154 % \textbf{Standard value:} $y$
155 % \end{macro}
156 %
157 % \section{Example}
158 %
159 % This documentation loaded the package with:
160 % \begin{verbatim}
161 % \usepackage[first=10, last=20]{lcg}\end{verbatim}
162 % The lines
163 % \begin{verbatim}
164 % \rand\arabic{rand} \rand\arabic{rand} \rand\arabic{rand}
165 % \rand\arabic{rand} \rand\arabic{rand} \rand\arabic{rand}
166 % \rand\arabic{rand} \rand\arabic{rand} \rand\arabic{rand}\end{verbatim}
167 % generate these numbers (all between 10 and 20):
168 % \rand\arabic{rand} \rand\arabic{rand} \rand\arabic{rand}
169 % \rand\arabic{rand} \rand\arabic{rand} \rand\arabic{rand}
170 % \rand\arabic{rand} \rand\arabic{rand} \rand\arabic{rand}
171 %
172 % Now the counter \cntr{die} should simulate a die and hold
173 % random numbers from $1$ to $6$. In addition it is demonstrated
174 % how to switch off the output to the screen and to the \texttt{log}-file.
175 % This can achieved with one of the following lines:
176 % \begin{verbatim}
177 % \reinitrand[last=6, counter=die, quiet]\end{verbatim}
178 % \reinitrand[last=6, counter=die]
179 % After that, the numbers
180 % \rand\arabic{die} \rand\arabic{die} \rand\arabic{die}
181 % \rand\arabic{die} \rand\arabic{die} \rand\arabic{die}.
182 % are a product of:
183 % \begin{verbatim}
184 % \rand\arabic{die} \rand\arabic{die} \rand\arabic{die}
185 % \rand\arabic{die} \rand\arabic{die} \rand\arabic{die}\end{verbatim}
186 % As one can see, the key \meta{first} has been reset to $1$.
187 %
188 % The following lines will change the range to $-6$ to $+6$
189 % without modifying any other option:
190 % \begin{verbatim} \chgrand[first=-6]\end{verbatim} \chgrand[first=-6]
191 % Here the numbers
192 % \rand\arabic{die} \rand\arabic{die} \rand\arabic{die}
193 % \rand\arabic{die} \rand\arabic{die} \rand\arabic{die}
194 % are stored in a user defined counter and brought to paper
195 % by:
196 % \begin{verbatim}
197 % \rand\arabic{die} \rand\arabic{die} \rand{arabic}{die}
198 % \rand\arabic{die} \rand\arabic{die} \rand\arabic{die}\end{verbatim}
199 %
200 % At last, random numbers between $1$ and $12$ will
201 % be generated and stored in the standard counter \cntr{rand}.
202 % The seed will be set to $1234$. There will also be a warning because
203 % the name of the counter is set to \texttt{rand} which was already
204 % defined when calling the package:
205 % \begin{verbatim} \reinitrand[last=12, seed=1234]\end{verbatim}
206 % \reinitrand[last=12, seed=1234]
207 % \begin{verbatim}
208 % \rand\Roman{rand} \rand\Roman{rand} \rand\Roman{rand} \end{verbatim}
209 % These lines produce: \rand\Roman{rand} \rand\Roman{rand} \rand\Roman{rand}.
210 % When using other formats than \texttt{arabic} for printing,
211 % the desired numbers might not appear on the screen
212 % because these formats don't support the full range of $2^{31}-1$.
213 %
214 %
215 % \section{The Linear Congruential Generator}
216 %
217 % The linear congruential generator used produces
218 % a sequence of numbers $I_j$ in the range from
219 % $1$ to $m$ by following rule:
220 % $$ I_{j+1} = a I_j \mod m $$
221 % where $I_0$ is set to a arbitrary starting value (called ``seed'').
222 % The quality of this generator depends on the choice of the
223 % parameters $a$ and $m$.
224 % Another problem is that when implementing the algorithm as above,
225 % the multiplication might leave the range \LaTeX\ can deal with.
226 % The solution is Schrage's method [\textsc{W.~Press} et~al.
227 % \emph{Numerical Recipies in \texttt{C}}\@.
228 % 2nd edition. Cambridge University Press 1992] which allows
229 % to perform the multiplications without leaving the given range.
230 % This is done by introducing two variables $r$ and $q$:
231 % $$
232 % m = a q + r \qquad\mbox{with}\qquad
233 % q = \left[m/a\right]\quad\mbox{and}\quad r = m \mod a
234 % $$
235 % where $[\,\cdot\,]$ denotes the integer part of the argument.
236 % If $z$ is an integer and $0 \leq r < q$ and $0 < z < m-1$,
237 % then the following (in)equalities hold:
238 % \begin{eqnarray*}
239 % & 0 \leq a \cdot (z \mod q) \leq m-1\\
240 % & 0 \leq \left[m/a\right] \leq m-1\\
241 % & a z \mod m = \left\{
242 % \begin{array}{ll} a \cdot (z \mod q ) -
243 % \left[z/q\right] & \mbox{if the term is } \leq 0\\
244 % a \cdot (z \mod q ) -
245 % \left[z/q\right] + m & \mbox{otherwise}
246 % \end{array}\right.
247 % \end{eqnarray*}
248 %
249 % To exploit the whole possible range and guarantee good performance,
250 % $a$ and $m$ are set as follows:
251 % $a = 7^5 \; = 16807$ and $m = 2^{31}-1 \; = 2147483647$ and
252 % this gives $q=127773$ and $r=2836$.
253 %
254 %
255 % \section{The Code}
256 %
257 % \subsection{Checking for possible conflicts}
258 %
259 % The following lines check if the commands provided by
260 % this package are already defined:
261 %
262 % \begin{macrocode}
263 \@ifundefined{rand}{}
264 {\PackageWarning{lcg}{Command `rand' already defined}}
265 \@ifundefined{r@ndcountername}{}
266 {\PackageWarning{lcg}{Command `r@ndcountername'
267 already defined}}
268 % \end{macrocode}
269 % Checking internal commands:
270 % \begin{macrocode}
271 \@ifundefined{r@nd}{}
272 {\PackageWarning{lcg}{Command `r@nd' already defined}}
273 \@ifundefined{initr@nd}{}
274 {\PackageWarning{lcg}{Command `initr@nd' already defined}}
275 \@ifundefined{cutr@nger@nd}{}
276 {\PackageWarning{lcg}{Command `cutr@nger@nd' already defined}}
277 \@ifundefined{@rderr@nd}{}
278 {\PackageWarning{lcg}{Command `@rderr@nd' already defined}}
279 \@ifundefined{ProcessOptionsWithKVr@nd}{}
280 {\PackageWarning{lcg}{Command `ProcessOptionsWithKVr@nd'
281 already defined}}%
282 \@ifundefined{qui@t}{}
283 {\PackageWarning{lcg}{Command `qui@t' already defined}}
284 \@ifundefined{firstletterr@nd}{}
285 {\PackageWarning{lcg}{Command `firstletterr@nd' already defined}}
286 % \end{macrocode}
287 % Checking the used counters:
288 % \begin{macrocode}
289 \@ifundefined{c@f@rst}{}
290 {\PackageWarning{lcg}{Counter `f@rst' already defined}}
291 \@ifundefined{c@l@st}{}
292 {\PackageWarning{lcg}{Counter `l@st' already defined}}
293 \@ifundefined{c@cr@nd}{}
294 {\PackageWarning{lcg}{Counter `cr@nd' already defined}}
295 \@ifundefined{f@rst}{}
296 {\PackageWarning{lcg}{Existing command `f@rst' conflicts
297 with counter `f@rst'}}
298 \@ifundefined{l@st}{}
299 {\PackageWarning{lcg}{Existing command `l@st' conflicts
300 with counter `l@st'}}
301 \@ifundefined{cr@nd}{}
302 {\PackageWarning{lcg}{Existing command `cr@nd' conflicts
303 with counter `cr@nd'}}
304 % \end{macrocode}
305 %
306 %
307 % \subsection{Macros for (re)initialization}
308 %
309 % \begin{macro}{init}
310 % Set starting values for the parameters and counters
311 % to standard values or according to the provides keys
312 % \begin{macrocode}
313 \def\initr@nd{%
314 \def\r@ndcountername{rand}%
315 \newcount \f@rst
316 \newcount \l@st
317 \newcount \cr@nd
318 \pr@keysr@nd%
319 \ProcessOptionsWithKVr@nd{Init}%
320 \p@stkeysr@nd%
321 \@utputr@nd%
322 } % end of \def\initr@nd
323 % \end{macrocode}
324 % \end{macro}
325 % \begin{macro}{reinit}
326 % Sets the provided keys and resets all other options.
327 % \begin{macrocode}
328 \def\reinitrand{\@ifnextchar[\@reinitr@nd{\@reinitr@nd[]}}%
329 \def\@reinitr@nd[#1]{%
330 \pr@keysr@nd%
331 \setkeys{Init}{#1}%
332 \p@stkeysr@nd%
333 \@utputr@nd%
334 }% end of \def\reinitrand
335 % \end{macrocode}
336 % \end{macro}
337 %
338 % \begin{macro}{chgrand}
339 % Sets the provided keys and doesn't change any other option.
340 % \begin{macrocode}
341 \def\chgrand{\@ifnextchar[\@chgr@nd{\@chgr@nd[]}}
342 \def\@chgr@nd[#1]{%
343 \@tempcnta = \z@
344 \@tempcntb = \z@
345 \setkeys{Init}{#1}%
346 \p@stkeysr@nd%
347 \@utputr@nd%
348 } % end of \def\chgrand
349 % \end{macrocode}
350 % \end{macro}
351 %
352 %
353 % \subsection{The keys}
354 %
355 % \begin{macro}{use keys}
356 % The following lines are from the \texttt{geometry} package
357 % written by \textsc{Hideo Umeki} (who borrowed it from the
358 % \texttt{hyperref} package written by \textsc{Sebastian Rahtz}).
359 % It enables the usage of the \meta{key}=\meta{value} scheme of the
360 % keyval package.
361 %
362 % \begin{macrocode}
363 \def\ProcessOptionsWithKVr@nd#1{%
364 \let\@tempa\@empty
365 \@for\CurrentOption:=\@classoptionslist\do{%
366 \@ifundefined{KV@#1@\CurrentOption}%
367 {}{\edef\@tempa{\@tempa,\CurrentOption,}}}
368 \edef\@tempa{%
369 \noexpand\setkeys{#1}{\@tempa\@ptionlist{\@currname.\@currext}}}
370 \@tempa
371 \AtEndOfPackage{\let\@unprocessedoptions\relax}}
372 % \end{macrocode}
373 % \end{macro}
374 %
375 % \begin{macro}{first}
376 % \begin{macrocode}
377 \define@key{Init}{first}[1]{\f@rst = #1}
378 % \end{macrocode}
379 % \end{macro}
380 % \begin{macro}{last}
381 % \begin{macrocode}
382 \define@key{Init}{last}[2147483647]{\l@st = #1}
383 % \end{macrocode}
384 % \end{macro}
385 % \begin{macro}{counter}
386 % \begin{macrocode}
387 \define@key{Init}{counter}[rand]{\def\r@ndcountername{#1}}
388 % \end{macrocode}
389 % \end{macro}
390 % \begin{macro}{seed}
391 % \begin{macrocode}
392 \define@key{Init}{seed}[\z@]{% seed for random number generator
393 \ifnum #1 < \z@%
394 \PackageWarning{lcg}{Seed should be > 0 --
395 Seed will be initialized with the actual time}%
396 \cr@nd = \z@%
397 \else%
398 \cr@nd = #1
399 \typeout{Random number generator initialized to #1}%
400 \fi%
401 }
402 % \end{macrocode}
403 % \end{macro}
404 % \begin{macro}{quiet}
405 % \begin{macrocode}
406 \define@key{Init}{quiet}[y]{
407 \def\qui@t{\expandafter\firstletterr@nd #1\delimiter}
408 \if \qui@t y% nothing to do
409 \else\if\qui@t Y \def\qui@t{y}
410 \else\if\qui@t j \def\qui@t{y}
411 \else\if\qui@t J \def\qui@t{y}
412 \else\if\qui@t n \def\qui@t{n}
413 \else\if\qui@t N \def\qui@t{n}
414 \else
415 \PackageWarning{lcg}{Value of key <quiet> must be <y> or <n>}
416 \def\qui@t{y}
417 \fi\fi\fi\fi\fi\fi
418 }
419 % \end{macrocode}
420 % \end{macro}
421 %
422 % \subsection{Macros called by other macros}
423 %
424 % \begin{macro}{pr@keys}
425 % The command \cmd{\pr@keysr@nd} is used to define and initialize
426 % all parameters (counters) needed by this package (before
427 % the keys are evaluated).
428 % Random numbers will be generated from $\mathtt{f@rst}$ to
429 % $\mathtt{f@rst} + \mathtt{l@st} - 1$,
430 % \cntr{cr@nd} will hold the random numbers
431 % (full range: $1$ to $2^{31}-1$) and \cntr{rand}
432 % will hold the random numbers (user defined range).
433 % The counters are also initialized to standard values.
434 % If the counter \cntr{cr@nd} equals zero, the seed will be
435 % initialized according to the actual time by the command \cmd{\r@nd}:
436 %
437 % \begin{macrocode}
438 \def\pr@keysr@nd{%
439 \f@rst = \@ne % 1
440 \l@st = 2147483647 % 2147483647
441 \cr@nd = \z@ % 0
442 \@tempcnta = \z@
443 \@tempcntb = \z@
444 \def\r@ndcountername{rand}%
445 \def\qui@t{n}
446 } % end of newcommand\def\pr@keysr@nd
447 % \end{macrocode}
448 % \end{macro}
449 %
450 % \begin{macro}{p@stkeys}
451 % The command \cmd{\p@stkeysr@nd} is executed after the
452 % keys are evaluated as last step of the initialization.
453 % The setting of the counter \cntr{l@st} depends on weather
454 % the key \key{last} is set or not.
455 % and the counter (user defined or standard name) is created.
456 % \begin{macrocode}
457 \def\p@stkeysr@nd{%
458 \@rderr@nd% last < first -> swap
459 \cutr@nger@nd% range too big -> cut
460 \@ifundefined{c@\r@ndcountername}{\newcounter{\r@ndcountername}}%
461 {%
462 \PackageWarning{lcg}{Using an already existing
463 counter \r@ndcountername}%
464 }%
465 \setcounter{\r@ndcountername}{0}%
466 } % end of \def\p@stkeysr@nd
467 % \end{macrocode}
468 % \end{macro}
469 %
470 % \begin{macro}{firstletter}
471 % This macro is used to determine the first letter of the value
472 % of the key \key{quiet}.
473 % \begin{macrocode}
474 \def\firstletterr@nd#1#2\delimiter{#1}
475 % \end{macrocode}
476 % \end{macro}
477 %
478 % \begin{macro}{@utput}
479 % Output to log-file/screen
480 % \begin{macrocode}
481 \def\@utputr@nd{%
482 \if \qui@t y% do nothing
483 \else
484 \typeout{Smallest possible random number: \the\f@rst}%
485 \typeout{Largest possible random number: \the\l@st}%
486 \typeout{The pseudo random numbers will be stored
487 in the LaTeX counter `\r@ndcountername'}%
488 \fi
489 }
490 % \end{macrocode}
491 % \end{macro}
492 %
493 % \begin{macro}{@rder}
494 % If the value of the key \key{last} is less than
495 % the value of \key{first}, they will be exchanged.
496 % \begin{macrocode}
497 \def\@rderr@nd{%
498 \ifnum \l@st < \f@rst%
499 \PackageWarning{lcg}{Key `last' less than key `first'
500 -- swapped}%
501 \@tempcnta = \f@rst
502 \f@rst = \l@st
503 \l@st = \@tempcnta
504 \fi%
505 }% end of \def\@rderr@nd
506 % \end{macrocode}
507 % \end{macro}
508 %
509 % \begin{macro}{cutr@nge}
510 % If the given range of random numbers exceeds the possibilities
511 % of \LaTeX\ (the limit is $2^{31}-1$), then the value of the \TeX-counter
512 % \cntr{@tempcnta} will be less than zero and the right border will be adjusted.
513 % \begin{macrocode}
514 \def\cutr@nger@nd{%
515 \ifnum\l@st<\z@\else
516 \@tempcntb = -2147483646 % -2^31 + 2
517 \@tempcnta = \f@rst
518 \advance \@tempcntb \l@st
519 \multiply \@tempcntb \m@ne
520 \advance \@tempcnta \@tempcntb
521 \ifnum \@tempcnta < \z@%
522 \PackageWarning{lcg}{Range contains too many numbers
523 -- right border reset to largest possible value}%
524 \advance \l@st \@tempcnta
525 \fi%
526 \fi%
527 }% end of \checkr@ange
528 % \end{macrocode}
529 % \end{macro}
530 %
531 %
532 % \subsection{Macros for random number generation}
533 %
534 % \begin{macro}{rand}
535 % The command \cmd{\rand} calls the internal command
536 % \cmd{\r@nd} which stores s random number (full range)
537 % within the counter \cntr{cr@nd}.
538 % If the condition
539 % $$
540 % \mathtt{cr@nd} \leq (\mathtt{l@st}-\mathtt{f@rst}+1)
541 % \cdot \frac{2^{31}-1}{\mathtt{l@st}-\mathtt{f@rst}+1}
542 % $$
543 % holds, \cntr{cr@nd} will be transformed
544 % to the given range:
545 % $$
546 % \mathtt{f@rst}+ \mathtt{cr@nd} - (\mathtt{l@st}-\mathtt{f@rst}+1)
547 % \cdot \frac{\mathtt{cr@nd}}{\mathtt{l@st}-\mathtt{f@rst}+1}
548 % $$
549 % and the result stored in the corresponding counter
550 % and otherwise \cmd{\rand} calls itself till the condition
551 % is satisfied. It's important
552 % to notice that the result of the division of two integers is
553 % again an integer (the fraction part is lost)!
554 %
555 % \begin{macrocode}
556 \def\rand{%
557 \r@nd%
558 \@tempcnta
559 \@tempcntb
560 \@tempcnta = \f@rst
561 \@tempcntb = \l@st
562 \multiply \@tempcnta \m@ne
563 \advance \@tempcntb \@tempcnta
564 \advance \@tempcntb \@ne %l@st-f@rst+1
565 \@tempcnta = 2147483647
566 \divide \@tempcnta \@tempcntb
567 \multiply \@tempcnta \@tempcntb
568 \ifnum \cr@nd > \@tempcnta
569 \rand%
570 \else
571 \setcounter{\r@ndcountername}{\cr@nd}%
572 \@tempcnta = \cr@nd
573 \divide \@tempcnta \@tempcntb
574 \multiply \@tempcnta \@tempcntb
575 \multiply \@tempcnta \m@ne
576 \addtocounter{\r@ndcountername}{\@tempcnta}%
577 \addtocounter{\r@ndcountername}{\f@rst}%
578 \fi
579 } % end of \rand
580 % \end{macrocode}
581 % \end{macro}
582 %
583 % \begin{macro}{r@nd}
584 % The command \cmd{\r@nd} generates pseudo random numbers within
585 % the range $1$ to $2^{31}-1$ Schrage's method and stores them
586 % in the counter \cntr{cr@nd}:
587 %
588 % \begin{macrocode}
589 \def\r@nd{%
590 \ifnum \cr@nd < \@ne% then ... initialize generator
591 \cr@nd = \the\time
592 \advance \cr@nd \inputlineno
593 \multiply \cr@nd \value{page}
594 \advance \cr@nd \the\year
595 \multiply \cr@nd \the\month
596 \multiply \cr@nd \the\day
597 \advance \cr@nd \inputlineno
598 \if \qui@t y%
599 \else
600 \typeout{Random number generator initialized to \the\cr@nd}%
601 \fi
602 \r@nd%
603 \else % else ... generate new number
604 \@tempcnta = \cr@nd
605 \divide \@tempcnta 127773 % \@tempcnta = floor(z/q)
606 \@tempcntb = \@tempcnta % \@tempcntb = floor(z/q)
607 \multiply \@tempcnta -2836 % \@tempcnta = -r*floor(z/q)
608 \multiply \@tempcntb -127773 % \@tempcntb = -q*floor(z/q)
609 \advance \cr@nd \@tempcntb % cr@nd = z mod q
610 \multiply \cr@nd 16807 % cr@nd = a * (z mod q)
611 \advance \cr@nd \@tempcnta % cr@nd = a*z mod m
612 \ifnum \cr@nd < \z@%
613 \advance \cr@nd 2147483647 % cr@nd = (a*z mod m) > 0
614 \fi
615 \global\cr@nd=\cr@nd % persist the change outside current scope
616 \fi
617 }% end of \r@nd
618 % \end{macrocode}
619 % \end{macro}
620 %
621 %
622 % \subsection{Initialization}
623 %
624 % \begin{macrocode}
625 \initr@nd % initialize the package
626 % \end{macrocode}
627 %
628 % \iffalse
629 %</lcg>
630 % \fi