GeneralRandom Number Generator for Worksheets?

LaTeX specific issues not fitting into one of the other forums of this category.
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Random Number Generator for Worksheets?

Post by LaTexLearner »

Does LaTeX provide a way to insert random numbers into documents?

Say I wanted to create worksheets for little children so they could practice adding single digit numbers.

Ideally, they'd practice slightly different worksheets each time, in the same format, but with different numbers.

Sometimes the first question would be 2+3=___. But if the kid scored 15/20 and wanted to try again, I'd like to just print another document that mixed up the numbers so the first question on the next version might be 9+4=___ or ___+3=10.

Is this possible? If not, what would you suggest?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
PMouse
Posts: 3
Joined: Wed Apr 27, 2016 8:51 am

Re: Random Number Generator for Worksheets?

Post by PMouse »

Have you looked at pgfmath which provides the 'random' package (and others) that do PRNG stuff? Is that what you are looking for?

I've never used this, myself, but ran across it a few times thinking I could use it for something in the future.
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Re: Random Number Generator for Worksheets?

Post by LaTexLearner »

I haven't, but I'm going to check that out.

Thanks!
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Random Number Generator for Worksheets?

Post by LaTexLearner »

I've been reading through the TikZ & PGF Manual Part VIII but, honestly, I can't make sense of it. The technical jargon is far beyond my current knowledge.

Is there a more non-technical introduction to random number generation that anybody knows of?

For the time being, I would like to create simple integer worksheets so I don't have to type something like this out by hand over and over and over.

2-5=
+3+(+6)=
__-5=-11
28/__=-4
User avatar
Stefan Kottwitz
Site Admin
Posts: 10290
Joined: Mon Mar 10, 2008 9:44 pm

Random Number Generator for Worksheets?

Post by Stefan Kottwitz »

Here is a small example:

Code: Select all

\documentclass{article}
\usepackage{tikz}
\pgfmathsetseed{\number\pdfrandomseed}
\newcommand*{\NewNumbers}{%
  \pgfmathsetmacro{\A}{random(0,10)}
  \pgfmathsetmacro{\B}{random(0,10)}}
\begin{document}
\NewNumbers
\A +\B = ?

\NewNumbers
\A *\B = ?
\end{document}
\NewNumbers initializes to get new numbers for A and B. I chose random(0,10) for a random number between 0 and 10.

Stefan
LaTeX.org admin
rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

Random Number Generator for Worksheets?

Post by rais »

Hi,
I've fiddled around with Stefan's code a bit...

Code: Select all

\documentclass{article}
\usepackage{tikz}
\pgfmathsetseed{\number\pdfrandomseed}
\newcommand*\PlaceHolder{\rule{1.5em}{0.3pt}}
\newcommand*\MyOp{?}
\newif\ifRgoesF% reads: if result goes first
\newcommand*{\NewNumbers}{%
  \pgfmathsetmacro{\Op}{random(0,3)}% kind of operation + - * /
  \pgfmathsetmacro{\Le}{random(0,2)}% which part to leave out
  \pgfmathsetmacro{\A}{random(0,10)}%
  \pgfmathsetmacro{\B}{random(0,10)}%
  \ifnum\Op > 1
    \pgfmathsetmacro{\C}{int(\A*\B)}% multiplication (or division)
  \else
    \pgfmathsetmacro{\C}{int(\A+\B)}% addition (or substraction)
  \fi
  \ifcase \Op
    \renewcommand*\MyOp{+}%
    \RgoesFfalse % \A + \B = \C
  \or
    \renewcommand*\MyOp{-}%
    \RgoesFtrue % \C - \B = \A
  \or
    \renewcommand*\MyOp{\cdot}%
    \RgoesFfalse % \A * \B = \C
  \or
    \renewcommand*\MyOp{/}%
    \RgoesFtrue % \C / \B = \A
  \else
    \typeout{This should not have happened...don't know operation `\Op'}%
  \fi
  \[
    \ifcase \Le
      \PlaceHolder \MyOp \B = \ifRgoesF\A\else\C\fi
    \or
      \ifRgoesF
        \C \MyOp \PlaceHolder = \A
      \else
        \A \MyOp \PlaceHolder = \C
      \fi
    \or      
      \ifRgoesF\C\else\A\fi \MyOp \B = \PlaceHolder
    \else
      ? ?? ??? = ????
    \fi
  \]
}
\newcommand*\GetSomeEquations[1][20]{%
  \foreach \Lp in {1,...,#1}{%
    \NewNumbers
  }%
}
\begin{document}
\GetSomeEquations
\end{document}
The basic idea behind it is to calculate the result, say, of a multiplication, before converting this into a division to ensure the result of this division being an integer value.
Of course, I didn't catch `division by zero' yet (nor am I sure if I should). Consider \B randomly set to zero: the result (\C) of a multiplication will be set to zero, too, but in case of a division, you might get 0/0=__ or 0/__=(whatever \A was set to).
Then again, it might be interesting to see if your students can make any sense of one of those, as well.;)

KR
Rainer
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Re: Random Number Generator for Worksheets?

Post by LaTexLearner »

@stefan_k and @rais

Thank you for your posts! I will try to make sense of them later today.
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Random Number Generator for Worksheets?

Post by LaTexLearner »

Stefan_K wrote:

Code: Select all

\documentclass{article}
\usepackage{tikz}
\pgfmathsetseed{\number\pdfrandomseed}
\newcommand*{\NewNumbers}{%
  \pgfmathsetmacro{\A}{random(0,10)}
  \pgfmathsetmacro{\B}{random(0,10)}}
\begin{document}
\NewNumbers
\A +\B = ?

\NewNumbers
\A *\B = ?
\end{document}
I think I understand most of this except

1. \pgfmathsetseed{\number\pdfrandomseed}? What does that do?

2. Why must there be an asterisk (*) in \newcommand*{\Newnumbers}...
User avatar
Stefan Kottwitz
Site Admin
Posts: 10290
Joined: Mon Mar 10, 2008 9:44 pm

Random Number Generator for Worksheets?

Post by Stefan Kottwitz »

1. Initializing the random number generator with a value.

2. * chooses a version of \newcommand that doesn't allow paragraph breaks in an argument. Can help to avoid errors.

Stefan
LaTeX.org admin
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Random Number Generator for Worksheets?

Post by LaTexLearner »

Stefan_K wrote:1. Initializing the random number generator with a value.
Heh, what does that mean?

Doesn't {random(0,10)} already create a random value?
Post Reply