summaryrefslogtreecommitdiff
path: root/graphics/pgf/base/tex/pgfmathfunctions.random.code.tex
blob: da20ce024f0e4fd0e3785c7b6b5d3fcdf256c34e (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
% Copyright 2019 by Mark Wibrow
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Public License.
%
% See the file doc/generic/pgf/licenses/LICENSE for more details.

% This file defines the pseudorandom numbers.
%
% Version 1.414213 29/9/2007

% Pseudo-random number generation.
%
% See:
% \book@{pressetal1992,
%    author    = {William H. Press and Brian P. Flannery and Saul A.
%                 Teukolsky and William T. Vetterling},
%    title     = {Numerical Recipies in C},
%    edition   = {Second},
%    publisher = {Cambridge University Press}
% }
%
% See also, the documentation for the lcg package by Erich Janka:
% (http://www.ctan.org/tex-archive/macros/latex/contrib/lcg/lcg.pdf)
%
\def\pgfmath@rnd@m{2147483647}% LaTeX Maximum.

\begingroup
\c@pgfmath@counta=\time%
\multiply\c@pgfmath@counta by\year%
\ifnum\c@pgfmath@counta=0
  \c@pgfmath@counta=1
\fi
\xdef\pgfmath@rnd@z{\the\c@pgfmath@counta}% The seed.
\endgroup

% \pgfmathsetseed
%
% Explicitly set the seed for the generator
%
% #1 -> the new seed.
%
\def\pgfmathsetseed#1{%
    % Attention pgflibraryluamath.code.tex relies on the fact that
    % this method also modifies \pgfmathresult:
    \pgfmathparse{#1}\expandafter\pgfmath@setseed\pgfmathresult.0\pgfmath@stop
}%
\def\pgfmath@setseed#1.#2\pgfmath@stop{\def\pgfmath@rnd@z{#1}}%

% Alternative parameters - see Press et al (1992) p278-279,
% for discussion.
%
% a=16807 q=127773 r=2836
% a=48271 q=4488   r=3399
%
\def\pgfmath@rnd@a{69621}
\def\pgfmath@rnd@r{23902}
\def\pgfmath@rnd@q{30845}

% \pgfmathgeneratepseudorandomnumber
%
% A linear congruency generator for generating
% pseudo-random numbers. Generates numbers in
% the range 1 - 2^31-1.
%
\def\pgfmathgeneratepseudorandomnumber{%
    \begingroup%
        \c@pgfmath@counta=\pgfmath@rnd@z%
        \c@pgfmath@countb=\pgfmath@rnd@z%
        \c@pgfmath@countc=\pgfmath@rnd@q%
        \divide\c@pgfmath@counta by\c@pgfmath@countc%
        \multiply\c@pgfmath@counta by-\c@pgfmath@countc%
        \advance\c@pgfmath@counta by\c@pgfmath@countb
        \c@pgfmath@countc=\pgfmath@rnd@a%
        \multiply\c@pgfmath@counta by\c@pgfmath@countc%
        \c@pgfmath@countc=\pgfmath@rnd@q%
        \divide\c@pgfmath@countb by\c@pgfmath@countc%
        \c@pgfmath@countc=\pgfmath@rnd@r%
        \multiply\c@pgfmath@countb by\c@pgfmath@countc%
        \advance\c@pgfmath@counta by-\c@pgfmath@countb%
        \ifnum\c@pgfmath@counta<0\relax%
            \c@pgfmath@countb=\pgfmath@rnd@m%
            \advance\c@pgfmath@counta by\c@pgfmath@countb%
        \fi%
        \xdef\pgfmath@rnd@z{\the\c@pgfmath@counta}%
    \endgroup%
    \edef\pgfmathresult{\pgfmath@rnd@z}%
}

% \pgfmathrnd
%
% Generates a pseudo-random number between 0 and 1.
%
\pgfmathdeclarefunction{rnd}{0}{%
    \begingroup%
        \pgfmathgeneratepseudorandomnumber%
        \c@pgfmath@counta\pgfmathresult%
        \c@pgfmath@countb\c@pgfmath@counta%
        \divide\c@pgfmath@countb100001\relax% To get one.
        \multiply\c@pgfmath@countb-100001\relax%
        \advance\c@pgfmath@countb\c@pgfmath@counta%
        \advance\c@pgfmath@countb1000000\relax%
        \expandafter\pgfmathrnd@@\the\c@pgfmath@countb\pgfmath@%
        \pgfmath@returnone\pgfmath@x%
    \endgroup%
}%

\def\pgfmathrnd@@#1#2#3\pgfmath@{%
    \edef\pgfmath@temp{#2.#3}%
    \pgfmath@x=\pgfmath@temp pt\relax%
}%

% \pgfmathrand
%
% Generates a pseudo-random number between -1 and 1.
%
\pgfmathdeclarefunction{rand}{0}{%
    \begingroup%
        \pgfmathgeneratepseudorandomnumber%
        \c@pgfmath@counta\pgfmathresult%
        \c@pgfmath@countb\c@pgfmath@counta%
        \divide\c@pgfmath@countb200001\relax%
        \multiply\c@pgfmath@countb-200001\relax%
        \advance\c@pgfmath@countb\c@pgfmath@counta%
        \advance\c@pgfmath@countb-100000\relax%
        \ifnum\c@pgfmath@countb<0\relax%
            \advance\c@pgfmath@countb-1000000\relax%
        \else%
            \advance\c@pgfmath@countb1000000\relax%
        \fi%
        \expandafter\pgfmathrand@@\the\c@pgfmath@countb\pgfmath@%
        \pgfmath@returnone\pgfmath@x%
    \endgroup%
}%

\def\pgfmathrand@@#1#2#3#4\pgfmath@{%
    \ifx#1-%
        \edef\pgfmath@temp{-#3.#4}%
    \else%
        \edef\pgfmath@temp{#2.#3#4}%
    \fi%
    \pgfmath@x=\pgfmath@temp pt\relax%
}%

\pgfmathdeclarefunction{random}{...}{%
    \begingroup%
        \def\pgfmath@temp{#1}%
        \ifx\pgfmath@temp\pgfmath@empty%
            \pgfmathrnd@%
        \else%
            \expandafter\pgfmathrandom@@#1\pgfmath@stop%
        \fi%
        \pgfmath@smuggleone\pgfmathresult%
    \endgroup}

\def\pgfmathrandom@@{\futurelet\pgfmath@token\pgfmathrandom@@@}
\def\pgfmathrandom@@@{%
    \ifx\pgfmath@token\bgroup%
        \expandafter\pgfmath@random@@@@%
    \else%
        \expandafter\pgfmath@random@@@@@%
    \fi%
}
\def\pgfmath@random@@@@@#1\pgfmath@stop{%
    \pgfmathrandominteger\pgfmathresult{1}{#1}%
}
\def\pgfmath@random@@@@#1#2\pgfmath@stop{%
    \pgfmathrandominteger\pgfmathresult{#1}{#2}%
}

% \pgfmathrandominteger
%
% Return a 'randomly' selected integer in the range #2 - #3 (inclusive).
%
% #1 - a macro to store the integer (not a count register).
% #2 - the lower limit of the range.
% #3 - the upper limit of the range.
%
\def\pgfmathrandominteger#1#2#3{%
    \begingroup%
        \pgfmathsetcount\c@pgfmath@counta{#2}%
        \pgfmathsetcount\c@pgfmath@countb{#3}%
        \c@pgfmath@countc\c@pgfmath@countb%
        % OK. Maybe #2 > #3.
        \ifnum\c@pgfmath@counta>\c@pgfmath@countb\relax%
            \c@pgfmath@countc\c@pgfmath@counta%
            \c@pgfmath@counta\c@pgfmath@countb%
            \c@pgfmath@countb\c@pgfmath@countc%
        \fi%
        \c@pgfmath@countd\c@pgfmath@counta
        \advance\c@pgfmath@countc1\relax%
        \advance\c@pgfmath@countc-\c@pgfmath@counta%
        \pgfmathgeneratepseudorandomnumber%
        \c@pgfmath@counta\pgfmathresult\relax%
        \c@pgfmath@countb\c@pgfmath@counta%
        \divide\c@pgfmath@countb\c@pgfmath@countc%
        \multiply\c@pgfmath@countb-\c@pgfmath@countc%
        \advance\c@pgfmath@counta\c@pgfmath@countb%
        \advance\c@pgfmath@counta\c@pgfmath@countd
        \edef\pgfmathresult{\the\c@pgfmath@counta}%
        \pgfmath@smuggleone{\pgfmathresult}%
    \endgroup%
    \edef#1{\pgfmathresult}%
}

% \pgfmathdeclarerandomlist
%
% Create a list to be used with \pgfmathrandomelement.
%
% #1 - the name of the list
% #2 - a list of elements (e.g., {item-1}{item-2}...{item-n}).
%
\def\pgfmathdeclarerandomlist#1#2{%
    \def\pgfmath@randomlistname{#1}%
    \begingroup%
        \c@pgfmath@counta=1\relax%
        % {} is a possible random element so (locally)
        % redefine \pgfmath@empty.
        \def\pgfmath@empty{pgfmath@empty}%
        \expandafter\pgfmath@scanrandomlist#2{pgfmath@empty}}
\def\pgfmath@scanrandomlist#1{%
    \def\pgfmath@scanneditem{#1}%
    \ifx\pgfmath@scanneditem\pgfmath@empty%
        \advance\c@pgfmath@counta-1\relax%
        \expandafter\xdef\csname pgfmath@randomlist@\pgfmath@randomlistname\endcsname{\the\c@pgfmath@counta}%
        \endgroup%
    \else%
        \expandafter\gdef\csname pgfmath@randomlist@\pgfmath@randomlistname @\the\c@pgfmath@counta\endcsname{#1}%
        \advance\c@pgfmath@counta1\relax%
        \expandafter\pgfmath@scanrandomlist%
    \fi}

% \pgfmathrandomitem
%
% Return a 'randomly' selected element from a list.
%
% #1 - a macro to store the item.
% #2 - the name of the list.
%
\def\pgfmathrandomitem#1#2{%
    \pgfmath@ifundefined{pgfmath@randomlist@#2}{\pgfmath@error{Unknown random list `#2'}{}}{%
        \edef\pgfmath@randomlistlength{\csname pgfmath@randomlist@#2\endcsname}%
        \pgfmathrandominteger{\pgfmath@randomtemp}{1}{\pgfmath@randomlistlength}%
        \def#1{\csname pgfmath@randomlist@#2@\pgfmath@randomtemp\endcsname}}}