# Shuffle questions in xsim

As said in the title, I want to shuffle questions while using xsim and tasks packages. Here is the code I took from multiple choice questions with correct choice environment which is written by @cgnieder.

\documentclass[12pt,a4paper]{article}
\usepackage[inline]{enumitem}
\usepackage[no-files]{xsim}

\DeclareExerciseEnvironmentTemplate{item}
{\item[\textbf{\GetExerciseName~\GetExerciseProperty{counter}}]}
{}

\XSIMexpandcode{%
#1%
}

\ForEachUsedExerciseByID{%
}%
}

\xsimsetup{
exercise/template = item,
exercise/name = Q.,
exercise/the-counter = \arabic{exercise}.
}

label = \textbf{\Alph*.},
]{choice}[\choice]

\newenvironment{questions}
{\enumerate}
{\endenumerate}

%=========================================================
\begin{document}

\section{Problems}

\begin{questions}
\begin{exercise}
What is the product of $-2$ and $3$?
\begin{choice}(4)
\choice \answer{$-6$}
\choice $6$
\choice $5$
\choice $-5$
\end{choice}
\end{exercise}

\begin{exercise}
What is the sum of the sides of a polygon called?
\begin{choice}(2)
\choice Leg
\choice Area
\choice Volume
\end{choice}
\end{exercise}

\begin{exercise}
What is the sum of $-2$ and $-3$?
\begin{choice}(4)
\choice $-6$
\choice $6$
\choice $5$
\choice \answer{$-5$}
\end{choice}
\end{exercise}
\end{questions}

\end{document}


In addition, is it possible to shuffle locally? For more specific, assume that I have 10 questions and divide them into 2 parts, one contains questions 1 to 5, and one contains the rest. I want to shuffle each part separately and get the questions in order, for example, 2 3 1 4 5 and 8 6 10 9 7.

Here is an idea which first adds the exercises to a collection and then randomly outputs them which also shuffles them. Using several collections this would also enable to shuffle blocks of exercises separately.

\documentclass[12pt,a4paper]{article}
\usepackage{xsim}

label = \textbf{\Alph*.} ,
label-width = 15pt
]{choice}[\choice](4)

\DeclareExerciseEnvironmentTemplate{item}
{\description\item[\GetExerciseName~\GetExerciseProperty{counter}]}
{\enddescription}

\DeclareExerciseEnvironmentTemplate{space}
{\textbf{\GetExerciseProperty{counter}} }

\XSIMexpandcode{%
\SetExerciseProperty{solution-body}
#1%
}

\DeclareExerciseCollection{part-A}

\xsimsetup{
exercise/template = item ,
solution/template = space ,
exercise/name = Q. ,
exercise/the-counter = \arabic{exercise}. ,
exercise/collect ,
random/sort = false
}

\begin{document}

\collectexercises{part-A}
\begin{exercise}
What is the product of $-2$ and $3$?
\begin{choice}
\choice \answer{$-6$}
\choice $6$
\choice $5$
\choice $-5$
\end{choice}
\end{exercise}
\begin{exercise}
What is the sum of the sides of a polygon called?
\begin{choice}(2)
\choice Leg
\choice Area
\choice Volume
\end{choice}
\end{exercise}
\begin{exercise}
What is the sum of $-2$ and $-3$?
\begin{choice}
\choice $-6$
\choice $6$
\choice $5$
\choice \answer{$-5$}
\end{choice}
\end{exercise}
\begin{exercise}
Number four
\end{exercise}
\begin{exercise}
Number five
\end{exercise}
\collectexercisesstop{part-A}

\section{Problems}
\printrandomexercises[collection=part-A]{5}