# How to include random images?

I'd like to make LaTeX pick one image file randomly from a series of eight images, and include it. I found that ''lcg'' package can generate random numbers:

\usepackage{lcg}
\reinitrand[counter=sec,first=1,last=8]


So i expected this line to include one of the images randomly:

\includegraphics[width=0.6cm]{excl0\rand\arabic{sec}c.png}


Where the file names are excl01c.png, excl02c.png, and so on. The result was not like i have imagined, i got an error message:

Runaway definition?
.
! TeX capacity exceeded, sorry [main memory size=3000000].
<argument> Random number generator initiali
zed to \the \cr@nd


My question is what is the reason of this kind of error, and how to fix it, how to make LaTeX choose an image file randomly?

• Put \rand before \includegraphics – egreg Sep 16 '12 at 9:58
• wow egreg, you solved it easily. thank you! could you put it in an answer, then i can accept. – deeenes Sep 16 '12 at 10:03

The \rand instruction computes a random number and stores it in the sec counter. On the other hand, the argument to \includegraphics should be a file name (after macro expansion), not the set of instructions to compute it.

Just perform the computation before doing \includegraphics:

\rand\includegraphics[width=0.6cm]{excl0\arabic{sec}c.png}


## Note

\rand will do some computations which are not allowed in an "expansion only" context. The string passed as argument to \includegraphics (or \input, that uses the same mechanism) can contain macros, but these should be able to expand to characters only.

## Extensions

If you need to use files numbered from 01 to 20, say, you have to ensure that the command prints a padding zero for numbers less than 10; the LaTeX kernel provides \two@digits for this:

\makeatletter
\newcommand{\printrandom}{\two@digits{\thesec}}
\makeatother

\rand\includegraphics[width=0.6cm]{excl\printrandom c.png}


For more digits, you can emulate the behavior of \two@digits:

1. Two digits

\newcommand{\printrandom}{\ifnum\value{sec}<10 0\fi\arabic{sec}}

2. Three digits

\newcommand{\printrandom}{\ifnum\value{sec}<100 0\fi\ifnum\value{sec}<10 0\fi\arabic{sec}}


Use again \rand\includegraphics[width=0.6cm]{excl\printrandom c.png}

Use \rand before of \includegraphics and the \thesec in the file name do de job. In the MWE the images1.png to image4.png are their respective handwritten numbers:

    \documentclass{article}
\usepackage{lcg}
\reinitrand[counter=sec,first=1,last=4]
\usepackage{graphicx}

\begin{document}

\rand\includegraphics[width=3em]{image\thesec}
\rand\includegraphics[width=3em]{image\thesec}
\rand\includegraphics[width=3em]{image\thesec}
\rand\includegraphics[width=3em]{image\thesec}
\rand\includegraphics[width=3em]{image\thesec}
\rand\includegraphics[width=3em]{image\thesec}
\rand\includegraphics[width=3em]{image\thesec}
\rand\includegraphics[width=3em]{image\thesec}
\rand\includegraphics[width=3em]{image\thesec}

\end{document}


• I really answered too late for making the MWE, but perhaps a MWE is easiest that the perfect explanation of @egreg :) – Fran Sep 16 '12 at 11:22
• This is exactly what @egreg already said both in his comment and his answer... – cgnieder Sep 16 '12 at 11:49
• @cgneider, I recognise that egreg has been the fastest (and best) responder on this side of the Pecos (see my first comment). But I keep because the value of the MWE (I hope this is not regarded as a lack of etiquette). – Fran Sep 16 '12 at 15:56
• I simply wondered why you repeated an already given answer. But since it's been upvoted I guess it has been proven useful :) – cgnieder Sep 16 '12 at 15:58
• @cgneider, simply I saw his answer after sending my answer. These things happen when you are not an expert and takes time find the solution, make the MWE and check if it works. :( – Fran Sep 16 '12 at 16:20