15

I'm basically trying to create multiple versions of a worksheet, each with different values for the variables in each question. To do this I'm defining random variables, then each time I compile the values change and I print a "new" version of the worksheet.

I understand how to create the random variables by using \pgfmathsetseed{\pdfrandomseed} and defining my variables in my desired ranges as e.g. \def\A{pgfrandom{1,10}\pgfmathresult}

The problem I run into is that I want to generate an answer for each problem at the end of the worksheet. But it wants to do the calculation for the answer with a different random value than the one generated in the original question.

e.g. If I say

\def\A{pgfrandom{1,10}\pgfmathresult}

\def\B{pgfrandom{10,20}\pgfmathresult}

\def\answer{\A + \B, \pgfmathresult}

and I ask the question:

What is \A + \B?

\answer

It will produce something like....

What is 2 + 12?

5 + 19, 24

So how do I get it to display the answer a) without showing the calculation itself (i.e. 2 + 12) and b) how do I get it to not re-generate a new random variable when computing the answer?

Sorry, I hope my questions are clear. I'm new to all this. Any help is appreciated. Thanks!

flugufrels
  • 151
  • 1
  • 3
  • 1
    Welcome to TeX.SX! Please make your code compilable, starting with \documentclass{...} and ending with \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to help you. Help them help you: remove that one hurdle between you and a solution to your problem. – someonr Dec 26 '13 at 15:39

2 Answers2

18

Based on your question you could do this:

Code

\documentclass{article}

\usepackage{pgf}

\pgfmathsetseed{\number\pdfrandomseed} % provide seed for pseudo random generator % (change to constant, to get the same random numbers on every time compiling)

% new command to init variables \newcommand\initVariables{% \pgfmathsetmacro{\A}{random(1,10)}% \pgfmathsetmacro{\B}{random(10,20)}% }

\newcommand\answer{\pgfmathprint{int(\A + \B)}} % define command to calculate result

\begin{document} \initVariables % initialize variables (do this every time you need new numbers) What is $\A + \B$?

Result is \answer \end{document}

Result

What is 2 + 12?
Result is 14

Fun with layers ;)

You could use layers to hide the answer, until the solution layer is enabled (you didn't ask for it, so this is just for fun).

\documentclass{article}

\usepackage{pgf} \usepackage{ocg-p}

\pgfmathsetseed{\number\pdfrandomseed} % provide seed for pseudo random generator

% new command to init variables \newcommand\initVariables{% \pgfmathsetmacro{\A}{random(1,10)}% \pgfmathsetmacro{\B}{random(10,20)}% }

\newcommand\answer{\pgfmathprint{int(\A + \B)}} %define command to calculate result \begin{document} \initVariables%initialize variables (do this every time you need new numbers) What is $\A + \B$?

Result is \begin{ocg}{result layer}{1}{0} \answer \end{ocg}

\end{document}

result

someonr
  • 8,531
5

A sagetex solution using Sage. Set up variables in sagesilent mode. Access the variables in your document through \sage command while you typeset.

\documentclass[12point]{article}%
\usepackage{sagetex}%  use Sage for it's math ability
\pagestyle{empty} % remove the page numbers
\begin{document}
\begin{sagesilent}
var('x')
a = Integer(randint(1,9))
b = Integer(randint(1,9))
\end{sagesilent}
% *********** END DEFINE VARIABLES ****************
\noindent What's $\sage{a}+\sage{b}$? $\sage{a}\times\sage{b}$?    $\sage{a}\div\sage{b}$?\\
Answer: $\sage{a}+\sage{b}=\sage{a+b}$, $\sage{a}\times\sage{b}=\sage{a*b}$ and $\sage{a}\div\sage{b}=\sage{a/b}$\\
You can get a decimal for the division $\sage{a}\div\sage{b}\approx\sage{(a/b).n(digits=5)}$
\end{document}

Here's the output running in Sagemath Cloud: enter image description here

DJP
  • 12,451