exam class - permute mc answer choices, generate answer key
I know this problem well. I have my own package that does it, along with several documents full of crufty code. I can tell you my implementation:
Permuting answer choices:
- Use a
pgfmath
list to store the answer choices - Do a Knuth shuffle on that list to permute them
- Walk over the list printing out each choice
You might find the \pgfmathsetseed
command useful because you want randomized choices but the same randomized choices each time you compile! You might also find http://random.org/ good for getting sufficiently random seeds.
For generating multiple versions and answer keys I use docstrip
. My main file is a docstrip file like exam.dtx
and I use guards like
%<A>\pgfmathsetseed{1001} % seed for A version
%<B>\pgfmathsetseed{1010} % seed for B version
%<*key>
% commands that alter whether the key is shown
%</key>
to \generate
files like exam-A.tex
, exam-A-key.tex
, etc. Then you have one document per file without manually repeating code.
I don't use the exam
class but I think this could work in that.
In ConTeXt, the itemize
environment (which is equivalent to LaTeX's itemize
and enumerate
) supports a random
key. So, to get random permutation of options use. But, you have to use \startitem
... \stopitem
instead of \item
to capture the items.
\starttext
\startitemize[n,random]
\startitem First item \stopitem
\startitem Second item \stopitem
\startitem Third item \stopitem
\stopitemize
\stoptext
I haven't checked how good the randomizing algorithm is. You could, of course, implement this in LuaTeX very easily. (The ConTeXt solution predates lua, and is implemented in eTeX)
Although this is a relatively old question, a possible answer, without the use of any packages or classes, is described here. It relies on Python and a little bash scripting to produce the different versions. Questions and their possible answers are automatically shuffled in each version, and the key is printed at the end of the produced tex files (after the \end{document}).