[Tex/LaTex] Getting an arbitrary point of the circle in TikZ

tikz-pgf

I wrote a code using tkz-euclide to randomly pick a point on a circle and work from there as you can see in my question How to control label positions in tikz-euclide. I have seen several questions already about randomizing the choosing of points in TikZ such as Tangents to a circle from a point outside of it (tikz) and Extract x, y coordinate of an arbitrary point in TikZ. And I could see that it is not that easy to do this. But how do we do it with just TikZ?

Edit I am sorry if my question is not that clear. What I am looking for is a solution that is written with the usual TikZ commands (in combination maybe with some TeX/LaTeX macros) that randomly chooses a point on the circle just like what \tkzGetRandPointOn does.

Best Answer

Here is a very simple answer.

enter image description here

\documentclass[margin=5mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \coordinate (center) at (1,2);
  \def\radius{2.5cm}
  % a circle
  \draw (center) circle[radius=\radius];

  % a random point of the circle
  \fill[red] (center) ++(rand*180:\radius) circle[radius=2pt];
\end{tikzpicture}
\end{document}

The same idea in more detail:

enter image description here

\documentclass[margin=5mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  % choose a seed for pseudo-random generator
  \pgfmathsetseed{\pdfuniformdeviate 10000000}

  % define a circle (center=O and \radius)
  \coordinate (O) at (1,2);
  \def\radius{2.5cm}

  % draw this circle and its center
  \draw (O) circle[radius=\radius];
  \fill (O) circle[radius=2pt] node[below left] {O};

  % define a random point (A) on this circle
  \pgfmathsetmacro{\angleA}{rand*180}
  \path (O) ++(\angleA:\radius) coordinate (A);

  % draw (A) with a label
  \fill[red] (A) circle[radius=2pt] ++(\angleA:1em) node {A};
\end{tikzpicture}
\end{document}

To generate a pseudo-random number between -180 and 180, you can use one of the following expressions:

  • rand*180 (my solution)
  • rdn*360-180 (derived from Andrew Stacey's comment)
  • random(-180,180) (an integer value only!)

The default seed of pseudo-random generator is \time × \year. Thus, it changes each minute. To choose a seed changing more frequently, use something like:

\pgfmathsetseed{\pdfuniformdeviate 10000000}