To work around the bug of the random
function, you may defined a new randomfixed
function:
\tikzset{declare function={randomfixed(\a,\b) = int(random(0,int(\b-\a))+\a);}}
Now, you may generate a random integer between two boundaries with parentheses for negative integer:
\newcommand\randomint[2]{\bgroup%
\pgfmathsetmacro\myval{randomfixed(#1,#2)}%
\pgfmathsetmacro\final{(\myval < 0)?"(\myval)":\myval}%
\final\egroup%
}
You may generate random operators:
\def\ops{{"+","-","\times","/"}}
\newcommand\randomop{\bgroup\pgfmathsetmacro\op{\ops[int(rnd*4)]}\op\egroup}
To change the seed of the pseudo-random generator each second, you may use the \pdfuniformdeviate
macro:
\pgfmathsetseed{\pdfuniformdeviate 10000000}
All the code:
\documentclass{article}
\usepackage{tikz}
% fix bug with random(a,b)
\tikzset{declare function={randomfixed(\a,\b) = int(random(0,int(\b-\a))+\a);}}
% get random integer
\newcommand\randomint[2]{\bgroup%
\pgfmathsetmacro\myval{randomfixed(#1,#2)}%
\pgfmathsetmacro\final{(\myval < 0)?"(\myval)":\myval}%
\final\egroup%
}
% get random operator
\def\ops{{"+","-","\times","/"}}
\newcommand\randomop{\bgroup\pgfmathsetmacro\op{\ops[int(rnd*4)]}\op\egroup}
% choose random seed
\pgfmathsetseed{\pdfuniformdeviate 10000000}
\pagestyle{empty}
\begin{document}
\foreach \n in {1,...,10}{
$\randomint{-10}{10} \randomop{} \randomint{-10}{10} = $\par
}
\end{document}
And a result:

3+-9
is really confusing for kids (and adults also). What does it mean? is it3+(-9)
? Then why not write it as3-9
? – Nasser Jul 22 '16 at 22:49\pgfmathparse{rand*20-10}
? I don't know if this works as you want. – Manuel Jul 22 '16 at 23:09\pgfmathparse{rand*20-10}
gave me -19.76776, not an integer. – WeCanLearnAnything Jul 26 '16 at 3:30