# specify a range using random numbers

I am using the following code to illustrate double angle theorem and although it works some of the time, I world like a method to define the random numbers so it always takes the form, as shown in the screen shot.

\documentclass[border=3.141592=12mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
positioning,
quotes}

\usetikzlibrary {angles,quotes}
\usetikzlibrary{calc,positioning}

\pgfmathsetseed{\pdfuniformdeviate 10000000}

\begin{document}
\begin{tikzpicture}[
dot/.style = {circle, fill, inner sep=1pt, outer sep=0pt},
ang/.style = {draw=red, <->,
angle eccentricity=1.2,
}
]
\pgfmathsetmacro{\r}{2}
\pgfmathsetmacro{\R}{\r+0.3}
% circle
% triangles' corners coordinates and labels
\foreach \c/\l in {rand/A, rand/B, rand/C}  % define random coefficients
% for calculations of triangle's
% corners coordinates on circle
% and define corners names
{
\pgfmathsetmacro{\C}{2*pi*\c}               % calculate triangle coordinates
\node (\l) [dot] at (\C:\r) {};             % draw dots at triangle corners
% \draw[-Stealth, gray, very thin]          % draw arrows from circle center
% to triangle's corners,
% if not needed, just delete this line
(0,0)   -- (\l);
\path (\l) -- (\C:\R) node {\l};          % define corners labels coordinates,
% they are in direction of vector
% from circle origin to dot node
}
% triangle
\draw[cyan]
(B) -- (O);           % draw trangle
\draw[cyan]
(C) -- (O);
\draw[cyan]
(C) -- (A);
\draw[cyan]
(A) -- (B);

\draw (B) -- (O) -- (C)
"$\theta$"] {angle = B--O--C};

\draw (B) -- (A) -- (C)
"$\theta$"] {angle = B--A--C};

\end{tikzpicture}
\end{document}


• There are some things strange: Why do you use this rand here \foreach \c/\l in {rand/A, rand/B, rand/C} and not simply \pgfmathsetmacro{\C}{2*pi*rand}? But it seems, that it works every run, if you set trig format=deg (default) and then \pgfmathsetmacro{\C}{360*rand}
– cis
Jun 19 at 15:48
• Sorry cis but I have tried what you suggested and it is not working. I might have to give up and start again, I can't figure it out. Jun 19 at 16:32

Like this:

I do not know (yet) why arc of angle $\theta:1$ is concave instead to be convex. Anyway, now possible angles of triangle corners are limited to range around selected angle limited by 0.3*\rand,

Edit: Meanwhile @hpekristiansen suugest that problem with drawing angle can be omitted by use degrees instead of radians for angle units. Using them in the first version of code the following changes had to be done:

• delete code line trig format=rad, (now commented
• in loop replace \foreach \c/\l in {4/A, 1/B, -4/C} with \foreach \c/\l in {45/A, 180/B, 315/C}
• in calculation of triangle corners coordinates replace \pgfmathsetmacro{\C}{4/\c + 0.3*rand} with \pgfmathsetmacro{\C}{\c + 15*rand}
\documentclass[border=3.141592mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{angles, arrows.meta,
backgrounds,
positioning,
quotes}

\pgfmathsetseed{\pdfuniformdeviate 10000000}

\begin{document}
\begin{tikzpicture}[
dot/.style = {circle, fill, inner sep=1pt, outer sep=0pt},
ang/.style = {draw=green!50!black, fill=green!10,
angle eccentricity=0.9,
font=\footnotesize,
anchor=west
}
]
\pgfmathsetmacro{\r}{2}
\pgfmathsetmacro{\R}{\r+0.3}
% circle
% triangles' corners coordinates and labels
\foreach \c/\l in {45/A, 180/B, 315/C}{
\pgfmathsetmacro{\C}{\c + 15*rand}
\node (\l) [dot] at (\C:\r) {};
%
\path (\l) -- (\C:\R) node {\l};
}
% triangle
\draw[cyan, semithick] (A) -- (B) -- (C) -- (O) -- (A);
% angles labels
\scoped[on background layer]
{
\pic [ang, "$\theta_1$"]            {angle = C--B--A};
\pic [ang, "$\theta_2=2\theta_1$"]  {angle = C--O--A};
}
\end{tikzpicture}
\end{document}


The code is a bit shorter, removed are all not needed code.

• Thanks so much! Jun 19 at 17:31
• Apparently ang only works with the option trig format=deg. I think it would be better not to use trig format=rad at all here. Jun 19 at 18:07
• @hpekristiansen, indeed! I will correct answer ASAP. However, I wonder why with radians it not working. Is there problem problems with accuracy of calculations? Jun 19 at 18:51
• @PaulA, see edited answer. Now problem with angle ars is resolved. Jun 19 at 19:06

This is my take of the problem. Note that I moved O to the same side of the midpoint of arc BC. I also don't allow two points to be closer than 10^\circ, and made sure B and C were on opposite sides of A.

\documentclass[border=12mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
positioning,
quotes,calc}

\usetikzlibrary {angles,quotes}
\usetikzlibrary{calc,positioning}

\pgfmathsetseed{\pdfuniformdeviate 10000000}

\begin{document}
\begin{tikzpicture}[
dot/.style = {circle, fill, inner sep=1pt, outer sep=0pt},
ang/.style = {draw=red, <->,
angle eccentricity=1.2,
}
]
\pgfmathsetlengthmacro{\r}{2cm}
\pgfmathsetlengthmacro{\R}{\r+0.3cm}
% circle
% triangles' corners coordinates and labels
\pgfmathsetmacro{\a}{random(-180,180)}%
\pgfmathsetmacro{\b}{random(\a+10,\a+180)}% \b ? \a
\pgfmathsetmacro{\c}{random(\a-10,\b-350)}% \c < \a
\coordinate(A) at (\a: \r);
\coordinate(B) at (\b: \r);
\coordinate(C) at (\c: \r);
\coordinate(D) at ({0.5*(\b+\c)}: \r);
\node at (\a: \R) {A};
\node at (\b: \R) {B};
\node at (\c: \R) {C};
\node at ($(O)!0.3cm!(D)$) {O};
\draw[orange]
(B) -- (O) -- (C);           % draw trangle
\draw[cyan]
(B) -- (A) -- (C);
\draw pic [draw=green!50!black, fill=green!5, angle radius=4mm,opacity=0.5,
"$\phi$"] {angle = B--O--C};
\draw pic [draw=green!50!black, fill=green!5, angle radius=4mm,opacity=0.5,
"$\theta$"] {angle = B--A--C};

\draw[green] (B) arc[start angle=\b, end angle={\c+360}, radius=\r];
\end{tikzpicture}
\end{document}

• Thanks for the input but it doesn't seem to render correctly, I will have to look over the code incase I am missing something obvious. Jun 19 at 18:46