TIL: Randomness in Latex

Jan 30, 2023 • Arne Vogel

You can use \usepackage{pgffor} for randomness in LaTeX.

\usepackage{pgffor}
\pgfmathdeclarerandomlist{Options}{{option1}{option3}{option3}}
\pgfmathrandomitem{\randomlySelected}{Options}

This will select one random element from Options and populate \randomlySelected with it. \randomlySelected can they get used just as you would expect it to work. One thing I noticed though: the random item selection will pick the same element for ~15 seconds. So rebuilding will not necessarily give you another option, even if you rebuild multiple times, if you don’t wait for some amount of time.

Disclaimer: This is something I just learned so I might be completely wrong. If I am please let me know, my email is in the footer.