[Tex/LaTex] How to randomize content with examdesign package using Sweave

rrandom numberssweave

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{shortanswer}[title={\Large Short Answer (10 pts each)}]

\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{answer}
    \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{answer}
\end{question}

\end{shortanswer}

\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?

Best Answer

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}
  \begin{answer}
  \end{answer}
\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}
  \begin{answer}
  \end{answer}
\end{question}

\end{shortanswer}

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}')
    }
    @
\end{shortanswer}