# Randomly Shuffle Enumerated list using enumerate package

I am building a template for writing exams, and I'd like to randomly shuffle my enumerated lists at compile time. The answers to this question worked for that purpose, however I am using other functions to be able to switch between a 'Student View' and a view of the 'Key', and the solutions provided doesn't work with the functionality of the enumerate package which I used when constructing my other functionality.

Here is an MWE, without the randomize functionality

\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\usepackage{amsmath, enumerate, ifthen, multicol, tikz}

\newcommand*\circleAns[1]{\tikz[baseline=(char.base)]{
\node[shape=circle,draw,inner sep=2pt,color=ForestGreen] (char) {#1};}}

\makeatletter
\newcommand{\itemAns}{
\stepcounter{enum\romannumeral\@enumdepth}
\item[\circleAns{\textcolor{ForestGreen}{\@nameuse{label\@enumctr}}}]
\else \item
\fi
}
\makeatother

\begin{document}

\begin{multicols}{2}
\begin{enumerate}[a)]
\itemAns
\item
\item
\end{enumerate}

\begin{enumerate}[a)]
\itemAns
\item
\item
\end{enumerate}
\end{multicols}

\end{document}


Producing the output:

Ideally, the randomization functionality would allow me to continue using both \begin{enumerate}[<options>] and \itemAns, and would also allow me to set a seed for the random generation process for repeatability. Thank you in advance for your assistance.

• I answered a similar question before. Does it help? tex.stackexchange.com/questions/566202/… Nov 15, 2020 at 22:42
• Alan, thank you for your comment, it was helpful to see, though I will be implementing egreg's solution. Nov 16, 2020 at 21:25

Here's how you can shuffle the answers in a repeatable way, by fixing the seed. Devise a formula based on the student number to be fed as the seed.

\documentclass{article}
%\usepackage{xparse} % not needed with LaTeX 2020-10-01 or later
\usepackage{enumitem} % much more powerful than enumerate
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}

\smash{\tikz[baseline=(char.base)]{%
\node[shape=circle,draw,inner sep=2pt,color=ForestGreen] (char) {#1};
}}%
}

\ExplSyntaxOn
\sys_gset_rand_seed:n { 43221+32 } % a fixed number + the student number

{
\end{enumerate}
}{}

{
}
{
}

{
\seq_set_split:Nnn \l__shaun_answers_seq { \item } { #1 }
}

{
\peek_charcode_remove:NTF +
}

{
{
}
{
#1
}
}

\ExplSyntaxOff

\begin{document}

\subsection*{First question}
\item+ Correct
\item Incorrect (1)
\item Incorrect (2)
\item Incorrect (3)

\subsection*{Second question}
\item+ Correct
\item Incorrect (1)
\item Incorrect (2)
\item Incorrect (3)

• @Shaun expl3 functions and variables should always start with a “unique” prefix in order to avoid clashes with other people's code. I often use as prefix the nickname of the person who asked for the code, if possible. Nov 16, 2020 at 21:14
• I am having one problem where I am trying to reference to the answer item using \label{} and \ref{} and I am receiving errors. Would you have any suggestions for implementing this functionality? Nov 17, 2020 at 0:24