# Shuffle numbers in a loop

I want to have a document, where the exact same page will be printed for a certain number of pages and on every page there should be a unique number.

I know how to do so with a loop, but I don't want the numbers of random order, like 5,2,7,10,1,.... (without any double number and no number out of range).

Is it possible to do this in LaTeX? I would give a number and I get an shuffled array of numbers back which I can use within the loop? (I do not want to create the list by some external script since the number can be different).

Is there a way that LaTeX can do something like this? Thank you very much!

(Background: I want to make exams where the numbers should refer to the students in order to make an anonymous list in the end. When the numbers are in natural order, everyone can see the mark of their neighbors in the exam. At the moment I am shuffeling after printing)

Edit: At the moment, I'm using sort of this code. And I would like to have a solution that something is calculated a new quantity \randno, which I can used within the loop instead of examno.

\documentclass{article}
\newcounter{examno} \setcounter{examno}{1}
\newcommand{\numbermax}{7}

\begin{document}
\loop
Test. This is exam no. \arabic{examno} \pagebreak
\ifnum\value{examno}<\numbermax
\stepcounter{examno}
\repeat
\end{document}

• Can you give a little more info on how you would like to have the shuffle. You can still keep them in the range. I've used this tex.stackexchange.com/questions/70458/… for shuffling limited numbers some time ago. But I can't really get what the user input is and what the outcome is. – percusse Jun 15 '13 at 8:55
• I've specified the question. Thank you for the link. I think, the C# code from the hybrid answer from your link seems quite appropriate to solve my problem, but I don't know if such a functions exist in LaTeX – Markus Klein Jun 15 '13 at 9:25
• In this case, while it's impressive to see pure TeX solutions (cf Knuth shuffle), it's often more practical to take advantage of LaTeX's acceptability and write a Perl or Bash script to do the dirty work for you (or even C#). – Sean Allred Jun 15 '13 at 14:17
• You can also use a modified version of the luatex solution from the linked question. – Aditya Jun 15 '13 at 14:36

Thanks for all users in the comments. I have used the code of user percusse from How to randomly shuffle a sliced image?.

The full example solving my problem is here:

\documentclass{article}
\usepackage{tikz,xstring}

\def\mylist{}
\def\myrandlist{}

\newcommand{\numbermax}{17}

\foreach \x in {1,...,\numbermax}{ %Generate list
\xdef\mylist{\mylist\noexpand{\x\noexpand}}
}

\begin{document}

% Shuffle list
\pgfmathdeclarerandomlist{mynum}{\mylist} %Define the list
\foreach \x in {1,...,\numbermax}{
\pgfmathrandomitem{\mynum}{mynum} % Pick one from that list
\xdef\myrandlist{\myrandlist,\mynum} % place in the new list
\StrSubstitute{\mylist}{{\mynum}}{}[\sublist] % Delete that entry from the list
\global\let\mylist\sublist % Update the main list
\pgfmathdeclarerandomlist{mynum}{\mylist} % Redefine the list
}
\xdef\myrandlist{\myrandlist,} % puts comma at the end of list

\foreach \x in {1,...,\numbermax} {%Generate exam
\pgfmathtruncatemacro{\xnext}{\x+1}
Test. This is exam no. \StrBetween[\x,\xnext]{\myrandlist}{,}{,}

}

\end{document}


Thank you very much!! (I'm sorry for answering my own questions, but I thought someone might be interested in the whole code answering the question.)