# Random Variables that I can call in latex Beamer

I am creating slides in latex beamer for use with clicker questions. On each question I would like the numbers to be randomly changed every time I compile, however I want to be able to call the variable later so I can write a solution on a separate slide. For example I would like Lim as x goes to a of 1/(x-a) where the a is random by can use on the next slide. Is this possible?

• Welcome to TeX.SX! Please add what you've got so far (and if it's just a dummy) to help us help you. – TeXnician May 11 '17 at 19:14

Are you familiar with R ? If you are, I would suggest you to look for knitr:

https://yihui.name/knitr/demo/beamer/

http://faculty.washington.edu/gyollin/docs/simpleBeamerKnitrExample.Rnw

Everytime you compile it with R, you will get a new value if you generate a random number It is maybe overkill for just one example, but it can be useful for bigger projects. You can access the values afterward using \Sexpr{var_name}

Plenty of examples can be found on the website of its creator, Yihui Xie.

This code snippet generates a random number between 0 and 10. Nothing shows on the latex because of echo= FALSE

<<echo = FALSE>>=
a <- runif(1,min = 0, to = 10)
@


This is a MWE adapted from http://faculty.washington.edu/gyollin/docs/simpleBeamerKnitrExample.Rnw

\documentclass{beamer}
\title{MWE for knitr}

\begin{document}

\maketitle

\begin{frame}[fragile]
\frametitle{Generation of a random vector}
No code snippet is displayed.
Slides with code chunks need to be marked as "fragile"
<<echo = FALSE>>=
x <- rnorm(100)
mean.x <- mean(x)
@
\end{frame}

\begin{frame}[fragile]
\frametitle{Displaying the mean of the random vector}
The mean of the random vector is \Sexpr{mean.x}
\end{frame}

\end{document}

• That's a great option, but it does not really provide an answer to the question. Would you please make an example for the OP that does what he wants? – TeXnician May 12 '17 at 14:44

The following example uses pseudorandom integers. They most likely depend on the file itself. Changing your .tex-file will most likely change the random number. You will need to save these numbers to counters and remember their names. Please see http://www.ctan.org/pkg/lcg.

\documentclass{beamer}

\usepackage{amsmath}

\usepackage[first=0, last=127]{lcg}
\newcommand{\setCounterToRandom}[1]{\rand\setcounter{#1}{\arabic{rand}}}
\newcommand{\createNewRandomCounter}[1]{\newcounter{#1}\setCounterToRandom{#1}}

\newcounter{randomA}
\setCounterToRandom{randomA}%assigning a pseudo-random value.

\begin{document}

\begin{frame}
\newcounter{randomB}%works here, too.
\setCounterToRandom{randomB}%works here, too.
\createNewRandomCounter{randomC}%makes things simple.

Random Number A \therandomA

Random Number B \therandomB

Random Number C \therandomC

\begin{align*}
%This example even makes sense, when the random number is eight.
\lim_{\xi \to \therandomA}  \frac 1 {\xi-\therandomA}
&\neq \rotatebox[origin=c]{90}{$\therandomA$}
\end{align*}
\end{frame}

\begin{frame}
Random Number A \therandomA

Random Number B \therandomB

Random Number C \therandomC

\setCounterToRandom{randomA}

New Random Number A \therandomA

Old Random Number B \therandomB

Random Number C \therandomC
\end{frame}
\end{document}