# Random number generator for case distinctions

I would like to compile a snippet of code using a random case distinction. How can I implement that?

No-LaTeX pseudocode:

x = rand(1,4) // random number between 1 and 4
if( x == 1 )
else if( x == 2 )
else if( x == 3 )
else


What is the best way this can be implemented? I would like to use it within math environments as well.

• If you have an uptodate (2020) TeXlive then there is the random.tex file for generating and using random numbers. Apr 22, 2020 at 17:28

You can make a command that outputs a random item from a list.

The items must be braced if they contain more than one token.

There are two versions: the first defines a command (suitable for long lists), the second can be used inline.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand{\makerandomchoice}{mm}
{
\shuhalo_random_choice_make:Nn #1 { #2 }
}

\NewExpandableDocumentCommand{\randomchoice}{m}
{
\shuhalo_random_choice:n { #1 }
}

\cs_new_protected:Nn \shuhalo_random_choice_make:Nn
{
\cs_new:Npn #1
{
\shuhalo_random_choice:n { #2 }
}
}

\cs_new:Nn \shuhalo_random_choice:n
{
\tl_item:nn { #1 } { \int_rand:nn { 1 } { \tl_count:n { #1 } } }
}

\ExplSyntaxOff

\makerandomchoice{\talk}{
}
\makerandomchoice{\letter}{ABCDEFGHIJKLMNOPQRSTUVWXYZ}

\begin{document}

\talk

\talk

\talk

\talk

\talk

\letter\letter\letter\letter\letter\letter\letter\letter
\letter\letter\letter\letter\letter\letter\letter\letter

\randomchoice{{Pear}{Apple}{Plum}{Cherry}}

\randomchoice{{Pear}{Apple}{Plum}{Cherry}}

\randomchoice{{Pear}{Apple}{Plum}{Cherry}}

\randomchoice{{Pear}{Apple}{Plum}{Cherry}}

\end{document}


Spaces between items are ignored.

• Thanks! Is it possible to use such a capability inline? It seems that your solution requires putting content in the header section. I am thinking of something that replaces the uncomment-recompile-comment-cycle, so to speak. Apr 21, 2020 at 22:48
• @shuhalo I added the “inline” version. Apr 21, 2020 at 22:54
• This does the job well. Just out of curiousity, what is the random number generator used? I suspect it is biased on my system. Apr 21, 2020 at 23:56
• While sufficient for my purposes, it does not seem to mesh well with blank lines within the argument. I'd be curious how that can be fixed. Apr 21, 2020 at 23:57
• @shuhalo Use {+m} instead of {m} in both main definitions. Apr 22, 2020 at 7:36

If you know R and knitr, this can be achieved with some very similar to your pseudocode:

\documentclass{article}
\begin{document}
<<fruit-roulette,echo=F,cache=FALSE>>=
x    <- sample(1:4, 1)
talk <- if( x == 1 ) {"apples"} else {
if( x == 2 ) {"pears"}  else {
if( x == 3 ) {"plums"}  else {
if( x == 4 ) {"cherries"}}}}
@

Because $x=\Sexpr{x}$, let us talk about \Sexpr{talk}!

\end{document}


In each compilation the LaTeX text could change, being one possible result:

As you see here, the \Sexpr{} can be use in math and text modes, but take into account that the R output will be formatted according to LaTeX rules (e.g., being the foo R object "big apples", \Sexpr{foo} or $\mbox{\Sexpr{foo}}$ will produce just "big apples", but \Sexpr{talk} will produce "bigapples".