6

I've been trying to adapt the steps given here. how-to-generate-random-variables-then-use-in-calculations-in-pdflatex

My MWE is below (and I apologise for the code being a bit clunky!)

\documentclass[a4paper,12pt]{exam}
\usepackage{pgf}
\renewcommand{\arraystretch}{1.5}
\pgfmathsetseed{\number\pdfrandomseed} 
\newcommand\initVariables{%
\pgfmathsetmacro{\A}{random(-10,10)}%
\pgfmathsetmacro{\B}{random(-10,10)}%
\pgfmathsetmacro{\C}{random(-10,10)}%
\pgfmathsetmacro{\D}{random(-10,10)}%
\pgfmathsetmacro{\E}{random(-10,10)}%
\pgfmathsetmacro{\F}{random(-10,10)}%
\pgfmathsetmacro{\R}{random(1,10)}%
}

\newcommand\ansa{\pgfmathprint{int(\A + \R)}} \newcommand\ansb{\pgfmathprint{int(\B + \R)}} \newcommand\ansc{\pgfmathprint{int(\C + \R)}} \newcommand\ansd{\pgfmathprint{int(\D + \R)}} \newcommand\anse{\pgfmathprint{int(\E + \R)}} \newcommand\ansf{\pgfmathprint{int(\F + \R)}}

\begin{document} \begin{questions} \question \initVariables% initialize variables (do this every time you need new numbers) $y=x+\R$\[0.3cm] \begin{tabular}{*{7}{|c}|} \hline $x$&$\A$&$\B$& $\C$ & $\D$ & $\E$ & $\F$\ \hline $y$ & $\ansa$ & $\ansb$ & $\ansc$ & $\ansd$ &$\anse$ & $\ansf$\ \hline \end{tabular} \end{questions} \end{document}

The code compiles, but I have a string of zeros before the equation enter image description here

Is there a way to get rid of the zeros?

Thanks for your help!

bbujeya
  • 611

0 Answers0