# Random Permutations of List Items

I am creating worksheets for the unit circle and special triangles. It is easy to make a list of those angles and then randomly print one of them at a time an arbitrary number of times. That code is below.

But this practically guarantees repetition of some angles. What if I want a random permutation such that each item is selected once?

I've read some other posts about permutations here on TeX.StackExchange but the answers there all seemed highly complicated. I'm hoping that there is perhaps an easier solution now. :-)

\documentclass{article}

\usepackage{pgf}
\usepackage{pgffor}

\pgfmathdeclarerandomlist{UnitCircleAngles}
{
{0}{90}{180}{270}{360}
{45}{135}{225}{315}
{30}{60}{120}{150}{210}{240}{300}{330}
}

\begin{document}

\section*{Unit Circle Diagrams}

Draw a diagram of the angle in standard position for each of the following.

\foreach \x in {1,...,16}
{\pgfmathrandomitem{\TheAngle}{UnitCircleAngles}%
\xdef\TheAngle{\TheAngle}%
\noindent \TheAngle%

\vspace{0.25cm}%
}

\end{document}

• This would be a fun exercise to do in Lua, if you're willing to use LuaTeX (compile with the command lualatex instead of pdflatex). Even otherwise it's doable, just generate a random permutation (example) and print it out. Aug 25 '17 at 22:25
• Looks like there's no great way to work with arrays (mutate an element, etc.) in pgf, according to this answer. This only strengthens my conviction that it's not a great idea to write “programs” in TeX macros. Use a real programming language. Aug 25 '17 at 22:32
• Here is my humble contribution: tex.stackexchange.com/questions/346260/… Aug 25 '17 at 22:36

A very very crude but concise push/pop emulator with comma based separation using xstring

\documentclass{article}
\usepackage{tikz,xstring}
\def\myshuffle{}
\def\UnitCircleAngles{{0}{90}{180}{270}{360}{45}{135}{225}{315}%
{30}{60}{120}{150}{210}{240}{300}{330}}
\pgfmathdeclarerandomlist{UnitCircleAngles}{\UnitCircleAngles}

\begin{document}
% Shuffle list
\foreach\x in{1,...,17}{
\pgfmathrandomitem{\myang}{UnitCircleAngles}                   % Pick one from that list
\xdef\myshuffle{\myshuffle\ifnum\x>1,\fi\myang}                % place in the new list
\StrSubstitute{\UnitCircleAngles}{{\myang}}{}[\sublist]        % Delete that entry
\global\let\UnitCircleAngles\sublist                           % Update the main list
\pgfmathdeclarerandomlist{UnitCircleAngles}{\UnitCircleAngles} % Redefine the list
}
\myshuffle
\end{document} • This looks promising. It will take me some time to make sense of the code but I'll try using it real soon. Thanks! Aug 26 '17 at 21:43

Sagemath, now CoCalc, using thesagetex package makes this easy. As a computer algebra system, Sage has many mathematical commands built into it. And it can evaluate the answers, too.

\documentclass{article}
\usepackage{sagetex}
\begin{document}
\begin{sagesilent}
a=Permutations([0,90,180,270,360,45,135,225,315,30,60,120,150,210,240,300,330]).random_element()
\end{sagesilent}
\noindent A random permutation of a set of your set of $\sage{len(a)}$ angles is:\\
$\sage{a}$.\\\\
Question $1$: What is $\sin(\sage{a}^{\circ})$?\\
Question $2$: What is $\cos(\sage{a}^{\circ})$?\\\\
Answer to Question $1$ is $\sage{sin(a*pi/180)}$.\\
Answer to Question $2$ is $\sage{cos(a*pi/180)}$.\\
\end{document}


The output is shown below: The documentation on permutations is here. EDIT: Some more details, if you're interested. Knowing Sage can do permutations, I searched the site for permutation to find documentation linked above. That told me the command a and gave sample output, something like [2,3,1]. This is what's known as a list in Python, which is basically an array in other languages. IF I had typed the Permutation command into Sage, which you can try using a Sage Cell Server here it would give a list back. That list is being stored in variable a. The 1 command is done in sagesilent environment so it is in the background. The way to get the results into LaTeX is using \sage for numbers or \sagestr for strings. The command \sage{len(a)} tells Sage determine how many elements are in the list, which is typeset between dollar signs as it is numerical data. The command \sage{a} prints out the list. Now Python refers to a list of k elements as being in the 0th, 1st, ...(k-1)st places. So \sage{a} grabs the first element in my random permutation and \sage{a1} grabs the second element. Finally, we get Sage to calculate the answers, thereby minimizing the chances of a mistake. This is done with \sage{sin(a*pi/180)} since Sage assumes the angle is in radians. Now Sage calculates the sine of the first angle in our list and inserts it into the document. No in depth knowledge required, just some basic skills that come up again and again.

• sagetex seems to come up a lot. I guess I really ought to commit to learning Python so I can make sense of it, huh? Or is there some way to learn how to use the sagetex package without Python? Aug 26 '17 at 21:44
• 1. You don't need to learn Python, just some basics. The structure of a for loop, inserting a comment, changing the format of a number, etc. 2, The minimal Python you need to learn is easier than TeX. 3. The overwhelming benefits come from the Sage commands, such as the Permutation command used above, that makes complicated, multi-line TeX program a 1 line command. 4. Depending on your task, you can get by just using Sage commands. It's a matter of searching the documentation, like the EDIT comment above explained, and finding the appropriate command syntax and output.
– DJP
Aug 27 '17 at 0:37
• I know what for loops and comments are, but what did you mean by "changing the format of a number"? Float to string and back? Aug 28 '17 at 20:35
• For example, the answer is sqrt(2) and you need/want a decimal answer. Or you don't want the full decimal answer but want the answer with 2 places after the decimal. Or, like you mention, float to string and back.
– DJP
Aug 28 '17 at 22:21
• I tried to compile this in TeXMaker, but got something looking quite different. imgur.com/a/E3SXF ; is the CoCalc thing mandatory to make sagetex work? Sep 3 '17 at 21:25