| 1 |
% Copyright 2019 by Mark Wibrow |
| 2 |
% |
| 3 |
% This file may be distributed and/or modified |
| 4 |
% |
| 5 |
% 1. under the LaTeX Project Public License and/or |
| 6 |
% 2. under the GNU Public License. |
| 7 |
% |
| 8 |
% See the file doc/generic/pgf/licenses/LICENSE for more details. |
| 9 |
|
| 10 |
% This file defines the pseudorandom numbers. |
| 11 |
% |
| 12 |
% Version 1.414213 29/9/2007 |
| 13 |
|
| 14 |
% Pseudo-random number generation. |
| 15 |
% |
| 16 |
% See: |
| 17 |
% \book@{pressetal1992, |
| 18 |
% author = {William H. Press and Brian P. Flannery and Saul A. |
| 19 |
% Teukolsky and William T. Vetterling}, |
| 20 |
% title = {Numerical Recipes in C}, |
| 21 |
% edition = {Second}, |
| 22 |
% publisher = {Cambridge University Press} |
| 23 |
% } |
| 24 |
% |
| 25 |
% See also, the documentation for the lcg package by Erich Janka: |
| 26 |
% (http://www.ctan.org/tex-archive/macros/latex/contrib/lcg/lcg.pdf) |
| 27 |
% |
| 28 |
\def\pgfmath@rnd@m{2147483647}% LaTeX Maximum. |
| 29 |
|
| 30 |
\begingroup |
| 31 |
\c@pgfmath@counta=\time% |
| 32 |
\multiply\c@pgfmath@counta by\year% |
| 33 |
\ifnum\c@pgfmath@counta=0 |
| 34 |
\c@pgfmath@counta=1 |
| 35 |
\fi |
| 36 |
\xdef\pgfmath@rnd@z{\the\c@pgfmath@counta}% The seed. |
| 37 |
\endgroup |
| 38 |
|
| 39 |
% \pgfmathsetseed |
| 40 |
% |
| 41 |
% Explicitly set the seed for the generator |
| 42 |
% |
| 43 |
% #1 -> the new seed. |
| 44 |
% |
| 45 |
\def\pgfmathsetseed#1{% |
| 46 |
% Attention pgflibraryluamath.code.tex relies on the fact that |
| 47 |
% this method also modifies \pgfmathresult: |
| 48 |
\pgfmathparse{#1}\expandafter\pgfmath@setseed\pgfmathresult.0\pgfmath@stop |
| 49 |
}% |
| 50 |
\def\pgfmath@setseed#1.#2\pgfmath@stop{\def\pgfmath@rnd@z{#1}}% |
| 51 |
|
| 52 |
% Alternative parameters - see Press et al (1992) p278-279, |
| 53 |
% for discussion. |
| 54 |
% |
| 55 |
% a=16807 q=127773 r=2836 |
| 56 |
% a=48271 q=4488 r=3399 |
| 57 |
% |
| 58 |
\def\pgfmath@rnd@a{69621} |
| 59 |
\def\pgfmath@rnd@r{23902} |
| 60 |
\def\pgfmath@rnd@q{30845} |
| 61 |
|
| 62 |
% \pgfmathgeneratepseudorandomnumber |
| 63 |
% |
| 64 |
% A linear congruency generator for generating |
| 65 |
% pseudo-random numbers. Generates numbers in |
| 66 |
% the range 1 - 2^31-1. |
| 67 |
% |
| 68 |
\def\pgfmathgeneratepseudorandomnumber{% |
| 69 |
\begingroup% |
| 70 |
\c@pgfmath@counta=\pgfmath@rnd@z% |
| 71 |
\c@pgfmath@countb=\pgfmath@rnd@z% |
| 72 |
\c@pgfmath@countc=\pgfmath@rnd@q% |
| 73 |
\divide\c@pgfmath@counta by\c@pgfmath@countc% |
| 74 |
\multiply\c@pgfmath@counta by-\c@pgfmath@countc% |
| 75 |
\advance\c@pgfmath@counta by\c@pgfmath@countb |
| 76 |
\c@pgfmath@countc=\pgfmath@rnd@a% |
| 77 |
\multiply\c@pgfmath@counta by\c@pgfmath@countc% |
| 78 |
\c@pgfmath@countc=\pgfmath@rnd@q% |
| 79 |
\divide\c@pgfmath@countb by\c@pgfmath@countc% |
| 80 |
\c@pgfmath@countc=\pgfmath@rnd@r% |
| 81 |
\multiply\c@pgfmath@countb by\c@pgfmath@countc% |
| 82 |
\advance\c@pgfmath@counta by-\c@pgfmath@countb% |
| 83 |
\ifnum\c@pgfmath@counta<0\relax% |
| 84 |
\c@pgfmath@countb=\pgfmath@rnd@m% |
| 85 |
\advance\c@pgfmath@counta by\c@pgfmath@countb% |
| 86 |
\fi% |
| 87 |
\xdef\pgfmath@rnd@z{\the\c@pgfmath@counta}% |
| 88 |
\endgroup% |
| 89 |
\edef\pgfmathresult{\pgfmath@rnd@z}% |
| 90 |
} |
| 91 |
|
| 92 |
% \pgfmathrnd |
| 93 |
% |
| 94 |
% Generates a pseudo-random number between 0 and 1. |
| 95 |
% |
| 96 |
\pgfmathdeclarefunction{rnd}{0}{% |
| 97 |
\begingroup% |
| 98 |
\pgfmathgeneratepseudorandomnumber% |
| 99 |
\c@pgfmath@counta\pgfmathresult% |
| 100 |
\c@pgfmath@countb\c@pgfmath@counta% |
| 101 |
\divide\c@pgfmath@countb100001\relax% To get one. |
| 102 |
\multiply\c@pgfmath@countb-100001\relax% |
| 103 |
\advance\c@pgfmath@countb\c@pgfmath@counta% |
| 104 |
\advance\c@pgfmath@countb1000000\relax% |
| 105 |
\expandafter\pgfmathrnd@@\the\c@pgfmath@countb\pgfmath@% |
| 106 |
\pgfmath@returnone\pgfmath@x% |
| 107 |
\endgroup% |
| 108 |
}% |
| 109 |
|
| 110 |
\def\pgfmathrnd@@#1#2#3\pgfmath@{% |
| 111 |
\edef\pgfmath@temp{#2.#3}% |
| 112 |
\pgfmath@x=\pgfmath@temp pt\relax% |
| 113 |
}% |
| 114 |
|
| 115 |
% \pgfmathrand |
| 116 |
% |
| 117 |
% Generates a pseudo-random number between -1 and 1. |
| 118 |
% |
| 119 |
\pgfmathdeclarefunction{rand}{0}{% |
| 120 |
\begingroup% |
| 121 |
\pgfmathgeneratepseudorandomnumber% |
| 122 |
\c@pgfmath@counta\pgfmathresult% |
| 123 |
\c@pgfmath@countb\c@pgfmath@counta% |
| 124 |
\divide\c@pgfmath@countb200001\relax% |
| 125 |
\multiply\c@pgfmath@countb-200001\relax% |
| 126 |
\advance\c@pgfmath@countb\c@pgfmath@counta% |
| 127 |
\advance\c@pgfmath@countb-100000\relax% |
| 128 |
\ifnum\c@pgfmath@countb<0\relax% |
| 129 |
\advance\c@pgfmath@countb-1000000\relax% |
| 130 |
\else% |
| 131 |
\advance\c@pgfmath@countb1000000\relax% |
| 132 |
\fi% |
| 133 |
\expandafter\pgfmathrand@@\the\c@pgfmath@countb\pgfmath@% |
| 134 |
\pgfmath@returnone\pgfmath@x% |
| 135 |
\endgroup% |
| 136 |
}% |
| 137 |
|
| 138 |
\def\pgfmathrand@@#1#2#3#4\pgfmath@{% |
| 139 |
\ifx#1-% |
| 140 |
\edef\pgfmath@temp{-#3.#4}% |
| 141 |
\else% |
| 142 |
\edef\pgfmath@temp{#2.#3#4}% |
| 143 |
\fi% |
| 144 |
\pgfmath@x=\pgfmath@temp pt\relax% |
| 145 |
}% |
| 146 |
|
| 147 |
\pgfmathdeclarefunction{random}{...}{% |
| 148 |
\begingroup% |
| 149 |
\def\pgfmath@temp{#1}% |
| 150 |
\ifx\pgfmath@temp\pgfmath@empty% |
| 151 |
\pgfmathrnd@% |
| 152 |
\else% |
| 153 |
\expandafter\pgfmathrandom@@#1\pgfmath@stop% |
| 154 |
\fi% |
| 155 |
\pgfmath@smuggleone\pgfmathresult% |
| 156 |
\endgroup} |
| 157 |
|
| 158 |
\def\pgfmathrandom@@{\futurelet\pgfmath@token\pgfmathrandom@@@} |
| 159 |
\def\pgfmathrandom@@@{% |
| 160 |
\ifx\pgfmath@token\bgroup% |
| 161 |
\expandafter\pgfmath@random@@@@% |
| 162 |
\else% |
| 163 |
\expandafter\pgfmath@random@@@@@% |
| 164 |
\fi% |
| 165 |
} |
| 166 |
\def\pgfmath@random@@@@@#1\pgfmath@stop{% |
| 167 |
\pgfmathrandominteger\pgfmathresult{1}{#1}% |
| 168 |
} |
| 169 |
\def\pgfmath@random@@@@#1#2\pgfmath@stop{% |
| 170 |
\pgfmathrandominteger\pgfmathresult{#1}{#2}% |
| 171 |
} |
| 172 |
|
| 173 |
% \pgfmathrandominteger |
| 174 |
% |
| 175 |
% Return a 'randomly' selected integer in the range #2 - #3 (inclusive). |
| 176 |
% |
| 177 |
% #1 - a macro to store the integer (not a count register). |
| 178 |
% #2 - the lower limit of the range. |
| 179 |
% #3 - the upper limit of the range. |
| 180 |
% |
| 181 |
\def\pgfmathrandominteger#1#2#3{% |
| 182 |
\begingroup% |
| 183 |
\pgfmathsetcount\c@pgfmath@counta{#2}% |
| 184 |
\pgfmathsetcount\c@pgfmath@countb{#3}% |
| 185 |
\c@pgfmath@countc\c@pgfmath@countb% |
| 186 |
% OK. Maybe #2 > #3. |
| 187 |
\ifnum\c@pgfmath@counta>\c@pgfmath@countb\relax% |
| 188 |
\c@pgfmath@countc\c@pgfmath@counta% |
| 189 |
\c@pgfmath@counta\c@pgfmath@countb% |
| 190 |
\c@pgfmath@countb\c@pgfmath@countc% |
| 191 |
\fi% |
| 192 |
\c@pgfmath@countd\c@pgfmath@counta |
| 193 |
\advance\c@pgfmath@countc1\relax% |
| 194 |
\advance\c@pgfmath@countc-\c@pgfmath@counta% |
| 195 |
\pgfmathgeneratepseudorandomnumber% |
| 196 |
\c@pgfmath@counta\pgfmathresult\relax% |
| 197 |
\c@pgfmath@countb\c@pgfmath@counta% |
| 198 |
\divide\c@pgfmath@countb\c@pgfmath@countc% |
| 199 |
\multiply\c@pgfmath@countb-\c@pgfmath@countc% |
| 200 |
\advance\c@pgfmath@counta\c@pgfmath@countb% |
| 201 |
\advance\c@pgfmath@counta\c@pgfmath@countd |
| 202 |
\edef\pgfmathresult{\the\c@pgfmath@counta}% |
| 203 |
\pgfmath@smuggleone{\pgfmathresult}% |
| 204 |
\endgroup% |
| 205 |
\edef#1{\pgfmathresult}% |
| 206 |
} |
| 207 |
|
| 208 |
% \pgfmathdeclarerandomlist |
| 209 |
% |
| 210 |
% Create a list to be used with \pgfmathrandomelement. |
| 211 |
% |
| 212 |
% #1 - the name of the list |
| 213 |
% #2 - a list of elements (e.g., {item-1}{item-2}...{item-n}). |
| 214 |
% |
| 215 |
\def\pgfmathdeclarerandomlist#1#2{% |
| 216 |
\def\pgfmath@randomlistname{#1}% |
| 217 |
\begingroup% |
| 218 |
\c@pgfmath@counta=1\relax% |
| 219 |
% {} is a possible random element so (locally) |
| 220 |
% redefine \pgfmath@empty. |
| 221 |
\def\pgfmath@empty{pgfmath@empty}% |
| 222 |
\expandafter\pgfmath@scanrandomlist#2{pgfmath@empty}} |
| 223 |
\def\pgfmath@scanrandomlist#1{% |
| 224 |
\def\pgfmath@scanneditem{#1}% |
| 225 |
\ifx\pgfmath@scanneditem\pgfmath@empty% |
| 226 |
\advance\c@pgfmath@counta-1\relax% |
| 227 |
\expandafter\xdef\csname pgfmath@randomlist@\pgfmath@randomlistname\endcsname{\the\c@pgfmath@counta}% |
| 228 |
\endgroup% |
| 229 |
\else% |
| 230 |
\expandafter\gdef\csname pgfmath@randomlist@\pgfmath@randomlistname @\the\c@pgfmath@counta\endcsname{#1}% |
| 231 |
\advance\c@pgfmath@counta1\relax% |
| 232 |
\expandafter\pgfmath@scanrandomlist% |
| 233 |
\fi} |
| 234 |
|
| 235 |
% \pgfmathrandomitem |
| 236 |
% |
| 237 |
% Return a 'randomly' selected element from a list. |
| 238 |
% |
| 239 |
% #1 - a macro to store the item. |
| 240 |
% #2 - the name of the list. |
| 241 |
% |
| 242 |
\def\pgfmathrandomitem#1#2{% |
| 243 |
\pgfmath@ifundefined{pgfmath@randomlist@#2}{\pgfmath@error{Unknown random list `#2'}{}}{% |
| 244 |
\edef\pgfmath@randomlistlength{\csname pgfmath@randomlist@#2\endcsname}% |
| 245 |
\pgfmathrandominteger{\pgfmath@randomtemp}{1}{\pgfmath@randomlistlength}% |
| 246 |
\expandafter\def\expandafter#1\expandafter{\csname pgfmath@randomlist@#2@\pgfmath@randomtemp\endcsname}}} |