The Tikz manual has all sorts of different options regarding math, but the following is the first version which actually worked. Is there a better way to do this?
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcount\gaussF
\edef\gaussR{0}
\edef\gaussA{0}
\def\gauss{
\advance\gaussF by 1\relax
\ifodd\gaussF
\pgfmathrnd
\edef\temp{\pgfmathresult}
\pgfmathln{\temp}
\edef\temp{\pgfmathresult}
\pgfmathmultiply{-2}{\temp}
\edef\temp{\pgfmathresult}
\pgfmathsqrt{\temp}
\edef\gaussR{\pgfmathresult}%radius = $sqrt(-2*ln(rnd))$
\pgfmathrnd
\edef\temp{\pgfmathresult}
\pgfmathmultiply{360}{\temp}
\edef\gaussA{\pgfmathresult}%angle = $360*rnd$
\pgfmathcos{\gaussA}
\edef\temp{\pgfmathresult}
\pgfmathmultiply{\gaussR}{\temp}
\else
\pgfmathsin{\gaussA}
\edef\temp{\pgfmathresult}
\pgfmathmultiply{\gaussR}{\temp}
\fi
\pgfmathresult
}
\begin{document}
\noindent
\gauss\\
\gauss\\
\gauss\\
\gauss
\end{document}
Best Answer
I'm not sure that the algorithm in the question is correct, nevertheless it is certainly implemented in a sub-optimal manner. Although Jake's (now deleted) answer is readable it also has a huge overhead in calling the parser inside a function.
It is quite simple to use the lower level
pgfmath
macros (although I probably would be expected to say that).Either way, in both cases,
rnd
produces pseudo-random numbers on the interval[0,1]
so the possibility ofln(0)
must be dealt with.