# Random polar coordinates

I want do draw some random tiny circles inside a bigger one.

I use rnd at both coordinates (polar): ({30*rnd}:{2*rnd}) -- here I want to have tiny circles in the range [0;30] degrees. The ouput is random but not limited to 30 degrees.

Then I want a constant angle: ({30}:{2*rnd}). The output doesn't make any sense. Both coordinates are random.

Finally I want constant distance to the center: ({30*rnd}:{2}). Everything OK by the looks of it: it's an arc limited by the 30 degrees.

Here's the code:

\documentclass{article}
\usepackage{tikz}
\usepackage{scalefnt}
\usetikzlibrary{arrows,calc, positioning}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{2mm}

\begin{document}

\begin{tikzpicture}

\draw (0,0) circle(3);

\draw[dashed] (0:0) -- (30:3);
\pgfmathsetseed{\pdfuniformdeviate 1000000};
\foreach \iter in {1,2,...,50} {
\draw[fill=red] ({30*rnd}:{2*rnd}) circle(0.01);
}
\end{tikzpicture}

\begin{tikzpicture}

\draw (0,0) circle(3);

\draw[dashed] (0:0) -- (30:3);
\pgfmathsetseed{\pdfuniformdeviate 1000000};
\foreach \iter in {1,2,...,50} {
\draw[fill=red] ({30}:{2*rnd}) circle(0.01);
}
\end{tikzpicture}

\begin{tikzpicture}

\draw (0,0) circle(3);

\draw[dashed] (0:0) -- (30:3);
\pgfmathsetseed{\pdfuniformdeviate 1000000};
\foreach \iter in {1,2,...,50} {
\draw[fill=red] ({30*rnd}:{2}) circle(0.01);
}

\end{tikzpicture}
\end{document}


Any ideas?

## 2 Answers

Polar coordinates in TikZ can be specified using a single radius for a circular coordinate system, or using an x and a y radius for elliptical coordinate systems. Internally, the circular coordinate system is represented by an elliptical coordinate system with x radius = y radius. This works fine for static values, but if the radius is set to rnd, different random numbers are generated for the x radius and the y radius, resulting in the behaviour you observed.

You can work around this by calculating the random radius outside the \draw statement using \pgfmathsetmacro\radius{1+rnd}, and then using \radius in the coordinate specification:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}

\draw (0,0) circle(3);
\draw[dashed] (0:0) -- (30:3);
\pgfmathsetseed{1};
\foreach \iter in {1,2,...,50} {
\pgfmathsetmacro\radius{1+rnd}
\draw[fill=red] (rnd*30:\radius) circle(0.01);
}
\end{tikzpicture}

\begin{tikzpicture}

\draw (0,0) circle(3);

\draw[dashed] (0:0) -- (30:3);
\pgfmathsetseed{\pdfuniformdeviate 1000000};
\foreach \iter in {1,2,...,50} {
\pgfmathsetmacro\radius{2*rnd}
\draw[fill=red] ({30}:{\radius}) circle(0.01);
}
\end{tikzpicture}

\begin{tikzpicture}

\draw (0,0) circle(3);

\draw[dashed] (0:0) -- (30:3);
\pgfmathsetseed{\pdfuniformdeviate 1000000};
\foreach \iter in {1,2,...,50} {
\draw[fill=red] ({30*rnd}:{2}) circle(0.01);
}

\end{tikzpicture}
\end{document}

• it definitely looks like a bug :) but I'm new to tikz so I didn't risk the assumption in first place :) thank you! – maf Jun 14 '13 at 19:37
• @maf: I figured out why this happens! I've added an explanation to my answer. – Jake Jun 14 '13 at 19:50
• or \pgfmathresult directly – percusse Jun 14 '13 at 20:47
• @percusse: Yes! – Jake Jun 14 '13 at 20:49

With PSTricks (no bug):

\documentclass[pstricks,border=12pt]{standalone}
\pstVerb{realtime srand}
\SpecialCoor

\begin{document}
% case 1: random radius [0,2] unit and angle [0,30] degrees
\begin{pspicture}(-3,-3)(3,3)
\pscircle(0,0){3}
\psLoop{50}{\pscircle(!rand 201 mod 100 div rand 301 mod 10 div PtoC){.01}}
\end{pspicture}

% case 2: random radius [0,2] unit and fixed angle 30 degrees
\begin{pspicture}(-3,-3)(3,3)
\pscircle(0,0){3}
\psLoop{50}{\pscircle(!rand 201 mod 100 div 30 PtoC){.01}}
\end{pspicture}

% case 3: fixed radius 2 unit and random angle [0,30] degrees
\begin{pspicture}(-3,-3)(3,3)
\pscircle(0,0){3}
\psLoop{50}{\pscircle(!2 rand 301 mod 10 div PtoC){.01}}
\end{pspicture}
\end{document}


## Outputs:

• Both radius and angle are randomly specified

• Random radius and fixed angle

• Fixed radius and random angle

• As the radius of the small circle is too small, it is better to use \qdisk instead of \pscircle. – kiss my armpit Jun 14 '13 at 20:40