# “Roll” random dice in footers, like in bookgames

I am playing with the gamebook package which allows to recreate the look and feel of gamebooks in LaTeX.

Many of these books had a couple of d6 dice at the bottom of each page, so that the reader could simulate a dice roll by simply opening a random page of the book to pass the challenges encountered during the adventure.

I'd like to recreate the same effect in my experiment: thanks to the fancyhdr package it's easy to modify the footer of the page to include some dice generated by means of the epsdice package.

Unfortunately, however, the \epsdice{<value>} command requires a value which is either a number from 1 to 6 or a variable containing such a value, and I have no clue how to randomly generate such value and pass it to the command.

Can anyone help?

Please find below a MWE mainly borrowed from the gamebook package example.

\documentclass[10pt,twoside]{article}

\usepackage{polyglossia}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[osf]{libertine}

\usepackage[dvipsnames]{xcolor}
\usepackage[debug,draft]{gamebook}
\usepackage{epsdice}

\usepackage{fancyhdr}
\fancyfoot[CE,CO]{\epsdice{2}\epsdice{5}} %% MODIFY THIS
\fancyfoot[LE,RO]{\thepage}

\title{Example for using the \textsf{gamebook} package}
\author{}
\date{}

\renewcommand{\gbturntext}{turn~to~}

\begin{document}

\maketitle

\pagenumbering{arabic}

\section*{Introduction}
This is an example for using the \textsf{gamebook} package that can be used for typesetting gamebooks with \LaTeX. If you do not know what a gamebook is, just have a look at the informative Wikipedia article
\begin{center}
\url{http://en.wikipedia.org/wiki/Gamebook}
\end{center}
or check out Demian's very good overview website, where you can find tons of information about gamebooks:
\begin{center}
\url{http://www.gamebooks.org}
\end{center}

The example text is a modified translation of the Spielbuch''-example on Wikipedia, \url{http://de.wikipedia.org/wiki/Spielbuch} used under CC-by-sa-3.0,  \url{http://creativecommons.org/licenses/by-sa/3.0/legalcode}. Many thanks go to the authors of that article.

\gbsection{start}
You are locked in a strange room. Right in front of you, there is a big red button with a bright sign above it. On the northern wall, there is a heavy steel door. What would like to do in order to escape this room?
\begin{gbturnoptions}
\gbitem{Press the red button}{redbutton}
\gbitem{Try to open the steel door}{steelclosed}
\end{gbturnoptions}

\gbsection{wrongsec}
Sections like this do not exist in typical gamebooks, because you cannot access them. In this example, such a section is only included in order to show you that you are reading the text in a wrong'' way. After section~1, you do not read directly section~2, but you proceed with the section you chose in section~1. This kind of interaction is what makes gamebooks a lot of fun. Now, please choose an option from section~1 above.

\gbsection{redbutton}
When you press the button, the whole building explodes. The game is over, please restart and remember: save early, save often.

\gbsection{steelopen}
You enter the code and a green light starts blinking. The heavy steel door opens. If you want to leave the room, \gbturn{steelleave}, if you would rather like to press the red button, \gbturn{redbutton}.

\gbsection{steelclosed}
The door is locked, but you find a small keypad right next to it. The pad's display reads ENTER CODE''. Do you know the right code? If so, turn to the section with the respective number. If you do not know the code, you can either press the red button (\gbturn{redbutton}) or read the sign above it (\gbturn{signread}).

\begin{quote}
\emph{This is a simple little example that shows the typical structure of a gamebook. The original version was written for the German Wikipedia. And by the way, the code for the heavy steel door is \ref{steelopen}'. Have fun and better do not press the red button!}''
\end{quote}

With this new and hopefully valuable information, you wonder what to do next. If you think the warning is just for fun and, thus, would like to press the red button, \gbturn{redbutton}. If you would like to examine the heavy steel door, \gbturn{steelclosed}.

\gbsection{steelleave}
Congratulations, you made it! Sunshine falls on your eyes and after you got used to the bright light, you see an angry \textsc{Orc} running towards you, a blood-stained knife ready in its claw. You have no choice but to defend yourself.

\gbvillain{Orc}{Skill}{5}{Stamina}{6}
The fight begins, but that is another story\dots

\end{document}

• Why do you need to generate the dice randomly? I would assume it better not to. Provided I open the book at a random page, I should get a random result, provided the book is a fair one (i.e. the proportions of each combination of faces should equal the probability of obtaining them when rolling fair dice). If you randomise the dice shown, you will likely generate an unfair book, equivalent to providing readers with unfair dice to roll. It is highly improbably you'll create a fair book, in fact. – cfr Jul 31 '17 at 2:22
• – John Kormylo Jul 31 '17 at 3:16
• Thanks for the constructive comments! 2 things: why do you say it is unfair? I don't know what pgfmath does, but I assume it generates random numbers with equal probability (compatibly with a pseudo- random mechanism); being each call independent, the distribution of the results should be fair especially if the number of pages is large enough. Do you refer to the fact that, for instance, on 37 pages you would have in theory one specific result that would appear twice as much as any other? Anyway, before answering, see my next comment... – Stefano Bragaglia Jul 31 '17 at 5:35
• Anyway, I just wanted to recreate the look and feel of those game books I had when I was a kid, and impress my friends showing them what LaTeX can do! Moreover, I don't have any of those books at hand to check, but I'm sure they weren't "fair". This is not the point, anyway, as randomly open a book is not fair for first: after a few times, the book tends to open on pages that were stretched open... loaded paper dice? ;-) Anyway the whole point is to recreate the fun experience, not a fair probabilistic model! – Stefano Bragaglia Jul 31 '17 at 5:42

It would be a little bit easier without xelatex, \pdfuniformdeviate could be used then or xfp's \fpeval, but I switched over to \pgfmathparse{random(6)} which generates an integer random number between 1 and 6 and stores the result in \pgfmathresult. This can be used in \epsdice as \epsdice{\pgfmathresult}.

In order to simplify this, I have made a tiny macro doing this: \myrandomdice.

Since \myrandomdice is called in the footer of the page during shipout, the dices are are random on each page (having the relevant pagestyle) and of course, the output of the 2nd page shown here will differ from a local compilation (unless setting the random seed explicitly)

Of course, the random numbers are only of pseudo-random nature. In order to set the seed, use \pgfmathsetseed{integer}, where integer stands for an arbitrary integer number.

\documentclass[10pt,twoside]{article}

\usepackage{polyglossia}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[osf]{libertine}

\usepackage[dvipsnames]{xcolor}
\usepackage[debug,draft]{gamebook}
\usepackage{epsdice}

\usepackage{pgf}

\newcommand{\myrandomdice}{%
\pgfmathparse{random(6)}\epsdice{\pgfmathresult}%
}

\usepackage{fancyhdr}
\fancyfoot[LE,RO]{\thepage}

\title{Example for using the \textsf{gamebook} package}
\author{}
\date{}

\renewcommand{\gbturntext}{turn~to~}

\begin{document}

\maketitle

\pagenumbering{arabic}

\section*{Introduction}
This is an example for using the \textsf{gamebook} package that can be used for typesetting gamebooks with \LaTeX. If you do not know what a gamebook is, just have a look at the informative Wikipedia article
\begin{center}
\url{http://en.wikipedia.org/wiki/Gamebook}
\end{center}
or check out Demian's very good overview website, where you can find tons of information about gamebooks:
\begin{center}
\url{http://www.gamebooks.org}
\end{center}

The example text is a modified translation of the Spielbuch''-example on Wikipedia, \url{http://de.wikipedia.org/wiki/Spielbuch} used under CC-by-sa-3.0,  \url{http://creativecommons.org/licenses/by-sa/3.0/legalcode}. Many thanks go to the authors of that article.

\gbsection{start}
You are locked in a strange room. Right in front of you, there is a big red button with a bright sign above it. On the northern wall, there is a heavy steel door. What would like to do in order to escape this room?
\begin{gbturnoptions}
\gbitem{Press the red button}{redbutton}
\gbitem{Try to open the steel door}{steelclosed}
\end{gbturnoptions}

\gbsection{wrongsec}
Sections like this do not exist in typical gamebooks, because you cannot access them. In this example, such a section is only included in order to show you that you are reading the text in a wrong'' way. After section~1, you do not read directly section~2, but you proceed with the section you chose in section~1. This kind of interaction is what makes gamebooks a lot of fun. Now, please choose an option from section~1 above.

\gbsection{redbutton}
When you press the button, the whole building explodes. The game is over, please restart and remember: save early, save often.

\gbsection{steelopen}
You enter the code and a green light starts blinking. The heavy steel door opens. If you want to leave the room, \gbturn{steelleave}, if you would rather like to press the red button, \gbturn{redbutton}.

\gbsection{steelclosed}
The door is locked, but you find a small keypad right next to it. The pad's display reads ENTER CODE''. Do you know the right code? If so, turn to the section with the respective number. If you do not know the code, you can either press the red button (\gbturn{redbutton}) or read the sign above it (\gbturn{signread}).

\begin{quote}
\emph{This is a simple little example that shows the typical structure of a gamebook. The original version was written for the German Wikipedia. And by the way, the code for the heavy steel door is \ref{steelopen}'. Have fun and better do not press the red button!}''
\end{quote}

With this new and hopefully valuable information, you wonder what to do next. If you think the warning is just for fun and, thus, would like to press the red button, \gbturn{redbutton}. If you would like to examine the heavy steel door, \gbturn{steelclosed}.

\gbsection{steelleave}
Congratulations, you made it! Sunshine falls on your eyes and after you got used to the bright light, you see an angry \textsc{Orc} running towards you, a blood-stained knife ready in its claw. You have no choice but to defend yourself.

\gbvillain{Orc}{Skill}{5}{Stamina}{6}
The fight begins, but that is another story\dots

\end{document}


• That was easy! I was missing how to pass the random integer to epsdice... Thanks a lot! – Stefano Bragaglia Jul 30 '17 at 22:40
• @StefanoBragaglia: You're welcome. Happy TeXing (and rolling the dices ;-)) – user31729 Jul 30 '17 at 22:55
• Neat solution to an ill-conceived problem statement :-). Shouldn't the aim be to create a fair book, to emulate the roll of fair dice? Yet this will very probably create an unfair book, emulating the roll of unfair dice. – cfr Jul 31 '17 at 2:26