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.

  • Welcome to tex.sx! If you indent lines by 4 spaces, then they are marked as a code sample. You can also highlight the code and click the "code" button (with "101010" on it). There is no need for manual <br/>s. – Caramdir May 5 '11 at 20:24
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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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