9.5⁂Random Numbers

The previous sections have looked at randomly selecting problems from a database, but it may be that you want to generate questions that use random numbers (for example, as coefficients) to make a slightly different problem each year. In addition to the random number command \PSNrandom provided by probsoln, both the fp and pgfmath packages provide a way of randomly generating numbers. In the case of the fp package, you can generate a random number between 0 and 1 using

\FPrandom{cs}

where ⟨cs⟩ is a control sequence in which to store the random number. The random number generator seed is set using

\FPseed=⟨number

(\FPseed is a count register.) For example

\FPseed=\year

will set the seed to the current year.

The pgfmath package provides

\pgfmathparse{expression}

which parses the given mathematical expression and sets \pgfmathresult to the result. There are a number of functions that may be used within ⟨expression⟩ (see the pgf user guide  for further details) but the random generator functions are

rnd

which generates a number between 0 and 1,

rand

which generates a number between  −1 and 1, and

random(⟨x⟩,⟨y⟩)

which generates a random integer between ⟨x⟩ and ⟨y⟩, if both are present, or a random integer between 1 and ⟨x⟩ if only ⟨x⟩ is present:

random(⟨x⟩)

or a random number between 0 and 1 if no arguments are present:

random()

The random number seed can be set using:

where ⟨n⟩ is an integer. For example:

Year: \the\year.
\pgfmathsetseed{\year}
\pgfmathparse{random(2,10)}
Random number: \pgfmathresult.

produces:

Additionally, the pgfmath package also provides:

\pgfmathrandominteger{cs}{minimum}{maximum}

which defines the control sequence ⟨cs⟩ to be a pseudo-randomly generated integer between ⟨minimum⟩ and ⟨maximum⟩ (inclusive). You can also define a list from which you want to randomly select an item. First you need to define the list using:

\pgfmathdeclarerandomlist{list name}{{item 1}{item 2}}

where ⟨list name⟩ is the name of the list and ⟨item 1⟩, ⟨item 2⟩ etc are the list items. (Note that this list isn't a comma-separated list. Each item is in braces like an argument.)

Once the list has been defined you can randomly select an item using:

\pgfmathrandomitem{cs}{list name}

where ⟨list name⟩ identifies the list. The result can then be accessed using the supplied control sequence ⟨cs⟩.

Example:

% define list
\pgfmathdeclarerandomlist{projects}%
{% list items
{ray-guns}% first item
{exploding chocolates}% third item
{telepathic cakes}% fourth item
}
% randomly select an item from the list
\pgfmathrandomitem{\thisproject}{projects}

What are the advantages and drawbacks of \thisproject?

As mentioned earlier, take care if you are using a mechanism that first displays questions and later (for example, at the end of the document) displays the solutions as this can cause a different randomly generated value in the solution. As before, I recommend that the question part globally defines a command that stores the randomly generated value which can later be accessed in the solution.

Example 48. Random Selection with pgfmath and probsoln

(Recall the commands \ifundef, \global and \let from §2.1.1 Macro Definitions.)

\documentclass{article}

\usepackage{pgfmath}
\usepackage{probsoln}

% set random seed
\pgfmathsetseed{\year}

\begin{defproblem}{easy.diff}%
\ifundef\easydiffcoeff
{%
\pgfmathrandominteger{\easydiffcoeff}{2}{10}% random coefficient
\global\let\easydiffcoeff\easydiffcoeff % make it global
}
\begin{onlyproblem}
% question
Differentiate with respect to $x$:
$y = \sin(\easydiffcoeff x)$
\end{onlyproblem}
\begin{onlysolution}
% solution
$y' = \easydiffcoeff\cos(\easydiffcoeff x)$
\end{onlysolution}
\end{defproblem}

\begin{document}

\section{Questions}

\begin{enumerate}
\foreachproblem{\item\thisproblem}
\end{enumerate}

\section{Solutions}
\begin{enumerate}
\foreachsolution{\item\thisproblem}
\end{enumerate}

\end{document}

This produces (where the year is 2014) the result shown in Figure 9.4. You can download or view this example.

Figure 9.4: Random Selection with pgfmath and probsoln

This book is also available as A4 PDF or 12.8cm x 9.6cm PDF or paperback (ISBN 978-1-909440-07-4).

© 2015 Dickimaw Books. "Dickimaw", "Dickimaw Books" and the Dickimaw parrot logo are trademarks. The Dickimaw parrot was painted by Magdalene Pritchett.