I would like to have a command (either user-defined, or from a package, or whatever) that has the basic form:
\selectNrandom{N}{list, of, comma, separated, elements}{code to execute}
which will select N distinct, random elements from the csv list that follows, and then executes the code on those elements.
For example, this would select two elements and then typeset them with a large space between them (think math quiz):
\selectNrandom{2}{N, W, Z, Q, R, C}{%
\mathbb{firstElement} \qquad \mathbb{secondElement}
}
I don't know what kind of MWE to post other than the above since I don't really have any idea of how to even begin writing a macro to do this.
Justification for Asking (i.e., I did do my homework!): I have been reading through sources such as various package documentations (etextools, probsoln, datatool, etc) as well as a few books (Joy of TeX, The Advanced TeXbook) and various websites. But I am still very new to the whole programming aspects of LaTex and Tex.
Best Answer
Here's a version with
xparse
and LaTeX3 code, with the help of therandom.tex
file by D. ArsenauThe macros take care to check that distinct elements are chosen by maintaining the list of already extracted elements and doing a new choice if a number is extracted again.
You refer to the first, second, and so on, element by
\htguse{1}
,\htguse{2}
and so on.The third call will raise an error:
With a recent
expl3
kernel,\input{random}
is not needed any longer withpdflatex
or LuaLaTeX (it still is necessary for XeLaTeX). The linecan be substituted with