# Solutions involving random values in the probsoln package

Over the past few years, I have constructed a large number of math questions which are stored in various external files. I access them for my quizzes via the probsoln package.

I typically construct the questions using quite a few randomly-generated values. The trouble comes when I try to access the solutions using the "second-pass" suggestion (the first pass producing the quiz, the second the answer key).

Here is a very abbreviated MWE.

\documentclass{article}
\usepackage{probsoln}

\newcounter{firstNum}
\newcounter{secondNum}

\begin{document}

Quiz:
\begin{enumerate}
\foreachproblem{%
\item \thisproblem
}
\end{enumerate}

\medskip

\begin{enumerate}
\foreachproblem{%
\item \thisproblem
}
\end{enumerate}
\end{document}


The question is stored in an external file, and has this format:

\newproblem{question1}{%
\random{firstNum}{1}{5}
\random{secondNum}{6}{10}

$$\thefirstNum + \thesecondNum=$$
}{%
}


When run, the output looks like this:

Quiz:

1. 2 + 9 =

1. 4 + 10 =

Solution: 4 + 10 = 14

Obviously the second iteration through has re-randomized the counter values. I need some way to preserve the selected values from being over-written so they can be accessed when the solutions are generated.

Another (possible) wrinkle is that most of my problems use the same counters over again. So the definition of question 2 would also assign random values to the counters. So it's not just a matter of preventing the solutions pass from overwriting the counter values, but the next question as well.

Thanks for any help you can give!

[EDIT]

I may have found a solution. It works with my MWE, but before I post it, I want to test it in a slightly more robust setting.

• I'm still trying to figure out an answer, but in the meantime, would someone venture a guess as to why the question isn't receiving up-votes? If it is a poor question I'll happily re-phrase or even retract it.
– HTG
Nov 12 '14 at 9:27

The following code seems to work, for simple cases at least. The first block of code is the quiz itself. It opens a write stream and loads the questions from an external file. The second block of code is the file containing the questions, and this is where the actual writing and reading of the solution is done.

\documentclass{article}

\usepackage{probsoln}

\newwrite\answerfile %Going to store current values in an external file

%Counters to hold randomly generated values for the math questions
\newcounter{firstNum}
\newcounter{secondNum}

\begin{document}
\loadallproblems{MWEQuestions}               %Load all problems from external file

%Start typesetting the quiz questions
Quiz:
\begin{enumerate}
\foreachproblem{%                            %Iterates through the loaded problems
\item \thisproblem                       %Typesets the current problem
}
\end{enumerate}
\immediate\closeout\answerfile                   %Done writing to the answer file
\medskip

\showanswers                                     %Boolean to allow typesetting the answers

% Start Answer Key
\begin{enumerate}
\foreachproblem{                            %Again, iterate through the loaded problems
\item \thisproblem
}
\end{enumerate}
\end{document}


Here is the code for the question file. Note that in order to see that multiple questions didn't cause problems the problem contains the definitions of two separate problems. In terms of code, these are identical (other than one is an addition question and the other a subtraction.

\newproblem{question1}{                                  %Define Question 1
\random{firstNum}{1}{5}                              %Randomly generate values
\random{secondNum}{6}{10}

\begin{onlyproblem}                                      %This code only runs if the answers
%are NOT being typeset
$$\thefirstNum + \thesecondNum=$$                    %Typeset the question for the quiz

%Write question to external answer file
\end{onlyproblem}

}{                                                      %This reads in the solution.
$\fileline$
}

%%% Second Question--Code Identical to the First
\newproblem{question2}{%                                % Define Question 2
\random{firstNum}{1}{5}
\random{secondNum}{6}{10}

\begin{onlyproblem}
$$\thefirstNum - \thesecondNum=$$ % Print the question

$\fileline$