#

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

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

(`\FPseed`

is a count register.)
For example

will set the seed to the current year.

The `pgfmath` package provides

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 [102] for further details) but the random generator
functions are

which generates a number between 0 and 1,

which generates a number between −1 and 1, and

`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:

`x`⟩)

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

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:

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:

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:

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 {mind-controlling cookies}%second 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?

**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 } {}%already been defined \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} \showanswers \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.

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