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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
    
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
up vote 9 down vote accepted
\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

share|improve this answer
    
I Thank you. It works great! – Efrain Soto May 5 '11 at 20:26

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.