# How to randomize content with examdesign package using Sweave

Forgive me in advance if this is a dumb question, or too specific to one package. I've been using examdesign to randomize the order of questions, but I also want to randomly generate the content. I've begun using Sweave to generate random numbers in R for some of the questions. However, when I ask for several different versions, it keeps the content the same from one version to the next. In other words, it first creates the random content from R, then it uses that same content across the several versions. What I want it to do is to call the R functions every time it creates a new version. Here's an example:

\documentclass[10pt]{examdesign}
\usepackage{Sweave}
\usepackage{amsmath}
\NumberOfVersions{2}

\begin{document}
\begin{examtop}
\renewcommand{\arraystretch}{1.5}
\begin{tabular}{ll}
Name:& \rule{2in}{.4pt}\\
\end{tabular}
\vspace{0.5in}
\begin{center}
\textbf{Introduction to Statistics} \\
\textbf{Practice Exam, Form \Alph{version}} \\
\end{center}
\end{examtop}

\begin{question}
<<ifst, echo=FALSE>>==
N = round(runif(1, 5, 20))
mu = 75
xbar = round(runif(1, 76, 84))
sig = round(runif(1, 5, 15))
z = round((xbar-mu)/(sig/sqrt(N)), digits=2)
p = round(1-pnorm(z), digits=4)
@

John wants to know whether his students are smarter than the average student.
John has \Sexpr{N} students and their average was \Sexpr{xbar}.
Assuming a population $\mu$ of \Sexpr{mu} and a standard deviation of
\Sexpr{sig}, can we conclude his students are smarter?

\examvspace*{1.5in}
\begin{align}
\nonumber Z_{obt} = \frac{\Sexpr{xbar}-\Sexpr{mu}}{\Sexpr{sig}/\sqrt{\Sexpr{N}}} =& \Sexpr{z} \\
\nonumber p_{obt} =& \Sexpr{p}
\end{align}
\end{question}

\end{document}


You'll notice that once you sweave it, there will be two versions, but the mean/sd/N will be identical for both versions. How do I get them to generate different random numbers?

-
Welcome to TeX.sx! A tip: You can use backticks  to mark your inline code as I did in my edit. –  Kurt Oct 26 '12 at 0:22
Do you have to use Sweave? Would a pgfmath solution be acceptable? –  Peter Grill Oct 26 '12 at 16:44
the R code only gets run once, upstream of the LaTeX processing, so this won't work. The brew package in R might have more capabilities for control of looping. I don't know the examdesign package, so I don't know precisely what the \NumberOfVersions{2} will do, but you could do the multiple versioning within R ... –  Ben Bolker Oct 26 '12 at 18:33
Sorry for the late reply...didn't realize someone had seen it! I'll look into the brew package. In the mean time, I've been just generating different versions more manually--I just have \NumberOfVersions{1}, then move the pdf each time. That way it will generate new content each time. A little less automated than I prefer, but it works. Thanks! –  Dustin Fife Nov 6 '12 at 1:55

Following Ben Bolker's comment:

Simply copy and paste the entire question environment. Edit the chunks' names. Sweave will then run the code twice and output the same text but with different numbers put in by \Sexpr.

\begin{shortanswer}[title={\Large Short Answer (10 pts each)}]

\begin{question}
<<ifst1, echo=FALSE>>==
N = round(runif(1, 5, 20))
@

John wants to know whether his students are smarter than the average student.
John has \Sexpr{N} students \dots

\examvspace*{1.5in}
\end{question}

\begin{question}
<<ifst2, echo=FALSE>>==
N = round(runif(1, 5, 20))
@

John wants to know whether his students are smarter than the average student.
John has \Sexpr{N} students \dots

\examvspace*{1.5in}
\end{question}



This works, but heavily violates the DRY principle. With knitr, a successor of Sweave, you could write R code that creates the entire question environment and writes it verbatim into the generated LaTeX code. (Perhaps this is possible with Sweave, too?) Something along these lines:

\begin{shortanswer}[...]
<<generate, echo=FALSE, format=asis>>=
for (i in 1:2) {
N = round(runif(1, 5, 20))
paste('\\begin{question}', 'John has', N, 'students', ..., '\\end{question}')
}
@

I believe that in Sweave the syntax is results=tex instead of format=asis` ... –  Ben Bolker Oct 27 '12 at 1:19