Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
2  
Could you explain for non-tkz users what \tkzGetRandPointOn does? Looking at Peter Grill's code, I would do something like \pgfmathsetmacro\ang{360*rnd} to get a random angle and then generate the coordinate from that using sin and cosine. Would that be enough, or do you need more? –  Loop Space Sep 11 '12 at 9:45
    
@GarbageCollector What random finite set? –  Loop Space Sep 11 '12 at 9:51
    
@AndrewStacey: The \tkzGetRandPointOn gets an arbitrary point of the target object. The code of Peter Grill was taken from my question in tex.stackexchange.com/questions/71072/… where I commented on each line to explain the code (from what I understand from the manual anyway). –  hpesoj626 Sep 11 '12 at 9:51
    
@hpesoj626 So to do \tkzGetRandPointOn for a circle, all you need to do is generate a random angle which \pgfmathsetmacro\ang{360*rnd} does. After that, it's just trigonometry. –  Loop Space Sep 11 '12 at 9:54
    
@GarbageCollector But only one of them is randomly chosen. The others are fixed. If you want to ensure that the random point is not near to the fixed points (so that the labels don't overlap) then that's easy enough: define an exclusion zone. –  Loop Space Sep 11 '12 at 9:57

2 Answers 2

up vote 8 down vote accepted

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}
share|improve this answer
    
\times x \year: So that is why the positions of the points do not change sometimes even after several compiles. :) –  hpesoj626 Sep 12 '12 at 2:12
    
@hpesoj626 With pdftex, you can use \pgfmathsetseed{\pdfuniformdeviate 1000000} to choose a more random seed. –  Paul Gaborit Sep 12 '12 at 6:24
    
That was nice of you to explain further. Thanks. –  hpesoj626 Sep 12 '12 at 6:50

Note that it is not TikZ solution and there is no guarantee that there is no duplicated points.

Case 1 (three fixed, one randomed)

enter image description here

\documentclass[pstricks,border=15pt]{standalone}
\usepackage{pst-eucl}

\pstVerb
{
    /randval {rand 36000 mod 100 div} def % random number from 0.00 to 359.99
}

\begin{document}
\psLoop{30}{%
\begin{pspicture}[showgrid=false](-2,-2)(2,2)
    \pnode(0,0){O}
    \pstGeonode[CurveType=polygon,linejoin=2]
    (2,0){A}
    (2;30){B}
    (2;70){C}
    (!2 randval PtoC){D}
    \pstCircleOA{O}{A}
\end{pspicture}}

\end{document}

Case 2 (all randomed)

enter image description here

\documentclass[pstricks,border=15pt]{standalone}
\usepackage{pst-eucl}

\pstVerb
{
    /randval {rand 36000 mod 100 div} def % random number from 0.00 to 359.99
}

\begin{document}
\psLoop{30}{%
\begin{pspicture}[showgrid=false](-2,-2)(2,2)
    \pnode(0,0){O}
    \pstGeonode[CurveType=polygon,linejoin=2]
    (!2 randval PtoC){A}
    (!2 randval PtoC){B}
    (!2 randval PtoC){C}
    (!2 randval PtoC){D}
    \pstCircleOA{O}{A}
\end{pspicture}}

\end{document}
share|improve this answer
    
(!2 rand 360 mod PtoC){D} do not need any additional definitions with \pstVerb –  Herbert Sep 11 '12 at 11:00
    
It looks simple enough in pstricks. I know that it is very powerful. I learned it first before I learned tikz but I can't say I understand it anymore. Looking back, I say that my reliance on tikz maybe mainly due to my heavy use of pdflatex. I have to revisit the manual. Thanks. –  hpesoj626 Sep 12 '12 at 1:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.