11

I am trying to generate fractals using affine transformations. I need to generate a random number in the interval (0,1), and depending on the value of this random number choose a function to calculate the coordinates of the point that I'll plot.

But the compiler is reporting an error I cannot figure out how to solve.

Here's a code that describes the situation.

\documentclass{article}

\usepackage{color}
\usepackage{tikz}
\usepackage{ifthen}

\begin{document}
%
\begin{center}
\begin{tikzpicture}
    \draw[help lines] (0,0) grid (5,5);
% Here I generate a random number
    \pgfmathparse{rnd}
% I store the random number generated in memory 
    \pgfmathsetmacro{\aleatorio}{\pgfmathresult}

    \ifthenelse{\aleatorio<0.5}{
        \node[red] at (5*rnd,5*rnd){\footnotesize\aleatorio};
    }{
        \node[blue] at (5*rnd,5*rnd){\footnotesize\aleatorio};
    }
\end{tikzpicture}
\end{center}
%
\end{document}

When it comes to the line of the command \ifthenelse to make the decision, it gives me an error message.

I think it is because \ifnum can compare only integer numbers. To be honest, I am not sure. In any case, I need help to compare nonintegers (with decimal part) numbers and then continue...

I appreciate your help.

Caramdir
  • 89,023
  • 26
  • 255
  • 291

1 Answers1

10
\ifthenelse{\lengthtest{\aleatorio pt<0.5pt}}{true}{false}

A better implementation would be with PGF functions:

\documentclass{article}

\usepackage{color}
\usepackage{tikz}
\usepackage{ifthen}

\begin{document}

\begin{center}
\begin{tikzpicture}
  \draw[help lines] (0,0) grid (5,5);
  % generate a random number and store it
  \pgfmathsetmacro{\aleatorio}{rnd}
  \pgfmathsetmacro{\rndcolor}{ \aleatorio<0.5 ? "red" : "blue" }
  \node[\rndcolor] at (5*rnd,5*rnd){\footnotesize\aleatorio} ;
\end{tikzpicture}
\begin{tikzpicture}
  \draw[help lines] (0,0) grid (5,5);
  % generate a random number and store it
  \pgfmathsetmacro{\aleatorio}{rnd}
  \pgfmathsetmacro{\rndcolor}{ \aleatorio<0.5 ? "red" : "blue" }
  \node[\rndcolor] at (5*rnd,5*rnd){\footnotesize\aleatorio} ;
\end{tikzpicture}
\begin{tikzpicture}
  \draw[help lines] (0,0) grid (5,5);
  % generate a random number and store it
  \pgfmathsetmacro{\aleatorio}{rnd}
  \pgfmathsetmacro{\rndcolor}{ \aleatorio<0.5 ? "red" : "blue" }
  \node[\rndcolor] at (5*rnd,5*rnd){\footnotesize\aleatorio} ;
\end{tikzpicture}
\begin{tikzpicture}
  \draw[help lines] (0,0) grid (5,5);
  % generate a random number and store it
  \pgfmathsetmacro{\aleatorio}{rnd}
  \pgfmathsetmacro{\rndcolor}{ \aleatorio<0.5 ? "red" : "blue" }
  \node[\rndcolor] at (5*rnd,5*rnd){\footnotesize\aleatorio} ;
\end{tikzpicture}
\end{center}

\end{document}

enter image description here

egreg
  • 1,121,712