| 1 |
\NeedsTeXFormat{LaTeX2e} |
| 2 |
\ProvidesPackage{fp-random}[1995/02/23] |
| 3 |
|
| 4 |
% Version information |
| 5 |
\def\FP@randomversion{1.0a} |
| 6 |
\message{% |
| 7 |
`Fixed Point Random,% |
| 8 |
\space\space\space\space\space\space% |
| 9 |
\space\space\space\space\space\space\space% |
| 10 |
Version \FP@randomversion% |
| 11 |
\space(C) Denis Girou (CNRS/IDRIS)% |
| 12 |
\space\space\space% |
| 13 |
} |
| 14 |
|
| 15 |
% Resolve dependencies |
| 16 |
\RequirePackage{fp-basic} |
| 17 |
|
| 18 |
% Uniform random value |
| 19 |
|
| 20 |
\newcount\FPseed % Seed value |
| 21 |
|
| 22 |
\def\FPrandom#1{% |
| 23 |
% #1 macro, which gets the result |
| 24 |
% |
| 25 |
% Uniform random number generator (numbers between 0 and 1) |
| 26 |
% |
| 27 |
% Algorithm reproduce from a very old Fortran program (unknown origin!) |
| 28 |
% |
| 29 |
% double precision function RANF() |
| 30 |
% |
| 31 |
% integer SEED |
| 32 |
% common /COMSEED/SEED |
| 33 |
% * |
| 34 |
% integer A,M,Q,R |
| 35 |
% parameter(A=16807,M=2147483647,Q=127773,R=2836) |
| 36 |
% * |
| 37 |
% integer LO,HI,TEST |
| 38 |
% * |
| 39 |
% HI = SEED/Q |
| 40 |
% LO = SEED-HI*Q |
| 41 |
% TEST = A*LO-R*HI |
| 42 |
% if(TEST.gt.0) then |
| 43 |
% SEED = TEST |
| 44 |
% else |
| 45 |
% SEED = TEST+M |
| 46 |
% endif |
| 47 |
% * |
| 48 |
% RANF = DFLOAT(SEED)/DFLOAT(M) |
| 49 |
% * |
| 50 |
% end |
| 51 |
% |
| 52 |
% The macro used a seed value, defined by the counter \FPseed. |
| 53 |
% If it's unknown at first call, we used an arbitrary value. |
| 54 |
% |
| 55 |
% We verify that we obtain the same results as in Fortran |
| 56 |
% |
| 57 |
% The algorithm seems fairly good. With the Fortran version, we obtain |
| 58 |
% typically something like 49954 numbers between 0 and 0.5 and 50046 between |
| 59 |
% 0.5 and 1 for 100000 random numbers, depending of the seed value used. |
| 60 |
% |
| 61 |
% To generate a "pseudo-random" seed, you can consider to use the \time |
| 62 |
% macro, to intialize the random numbers generator according to the time. |
| 63 |
% Something like: |
| 64 |
% \FPseed=\the\time |
| 65 |
% \multiply\FPseed\the\day |
| 66 |
% \multiply\FPseed\the\month |
| 67 |
% \multiply\FPseed\the\year |
| 68 |
% |
| 69 |
% Of course, it is very slow comparing to "standard" programming |
| 70 |
% languages. On a RS 6000 370, it's take 51s to generate 100 numbers, |
| 71 |
% loading time of the test program included (and 0.2s for the Fortran |
| 72 |
% version ... for 100000 numbers!) |
| 73 |
% But you must also notice that Hans van der Meer published in TUGboat |
| 74 |
% Vol. 15, 1, March 1994, pages 57-58, an article on "Random bit generator |
| 75 |
% in TeX". In fact, he doesn't give a complete solution to generate uniform |
| 76 |
% random numbers, but at least give some interesting ideas, and also the |
| 77 |
% code of a simple but powerful macro \SRtest{choice 1}{choice 2}, which |
| 78 |
% allow to execute the instructions of "choice 1" or those of "choice 2" |
| 79 |
% with a probability of 1/2 each. As his method is based on shifts of bits |
| 80 |
% with almost no computations, it's very fast (around 1000 faster than to do |
| 81 |
% the same thing that \SRtest with \FPrandom.) |
| 82 |
% |
| 83 |
{\FP@beginmessage{RANDOM}% |
| 84 |
% |
| 85 |
\ifnum\FPseed=0% |
| 86 |
\FPseed=123456789% |
| 87 |
\FP@debug{random: seed value undefined! We will used \the\FPseed.^^J% |
| 88 |
Define it if you want to generate a different sequence of random% |
| 89 |
numbers.}% |
| 90 |
\else% |
| 91 |
\FP@debug{random: seed value used: \the\FPseed}% |
| 92 |
\fi% |
| 93 |
% |
| 94 |
\FP@xia=\FPseed% |
| 95 |
\divide\FP@xia by 127773% |
| 96 |
\FP@xib=\FP@xia% |
| 97 |
\multiply\FP@xib by 127773% |
| 98 |
\advance\FP@xib by -\FPseed% |
| 99 |
\FP@xib=-\FP@xib% |
| 100 |
\multiply\FP@xia by 2836% |
| 101 |
\FPseed=\FP@xib% |
| 102 |
\multiply\FPseed by 16807% |
| 103 |
\advance\FPseed by -\FP@xia% |
| 104 |
% |
| 105 |
\ifnum\FPseed>0% |
| 106 |
\else% |
| 107 |
\advance\FPseed by 2147483647% |
| 108 |
\fi% |
| 109 |
\FPdiv\FP@tmpa{\the\FPseed}{2147483647}% |
| 110 |
\global\let\FP@tmp\FP@tmpa% |
| 111 |
\global\FPseed=\FPseed% |
| 112 |
\FP@debug{random: random number: \FP@tmp\space% |
| 113 |
(new seed value: \the\FPseed)}% |
| 114 |
% |
| 115 |
\FP@endmessage{}% |
| 116 |
}% |
| 117 |
\let#1\FP@tmp% |
| 118 |
} |
| 119 |
|
| 120 |
\endinput |