## Saturday, March 19, 2011

### Random numbers in TeX

• \pdfuniformdeviate num generates a uniformly distributed random integer in the range [0, num).
• \pdfnormaldeviate generates a normally distributed random integer with mean 0 and “a unit of 65536”. (I've never seen unit used that way, so I'm not sure exactly what the manual means.)

These are both expandable and can be used if you need random numbers for some reason. Here's one toy example that generates coinflips with a biased coin.
\def\coinflip#1{%
\ifnum#1>\pdfuniformdeviate1000
H%
\else
T%
\fi
}
\tt
\parindent=0pt
\raggedright
\newcount\n \n=0
\newcount\tails \tails=0
\loop\ifnum\n<1000
\if\coinflip{327}H%
H
\else
T
\fi
\repeat

\vskip\baselineskip
\rm
$p=0.327$\par
\bye