# How to Generate Random Negative Integer?

I am trying to create a worksheet constituted of random numbers so that kids can practice integer operations. E.g.

3+-9=?

2x(-5)=?

18/-3=?

Etc.

How do I do this?

Here is what I've tried so far.

\documentclass{minimal}

\usepackage{tikz}

\begin{document}

\pgfmathparse{random(-10,10)}
\pgfmathresult

\end{document}


Two problems.

1. Why is there an extra 0.0. before the integer?
2. Why is that random number 7 when my bounds are -10 and 10? Picking 7 repeatedly seems pretty non-random.

Some of this was mentioned in another thread but that thread is now a couple of years old.

Got ideas?

Thanks!

• 3+-9 is really confusing for kids (and adults also). What does it mean? is it 3+(-9)? Then why not write it as 3-9? – Nasser Jul 22 '16 at 22:49
• @Nasser Because in order to learn that if you add a negative number to a positive number it's the same as subtraction you need to be able to show examples of that. – Alan Munn Jul 22 '16 at 22:57
• \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

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:

• Above and beyond the question! That's why we all love this site. – Alan Munn Jul 23 '16 at 0:25
• Wow, this looks amazing! Thank you! I only knew I wish enough about TikZ and coding to understand this in just one read. :) I'll keep reading tryis to try to make sense of it. I'll be back posting questions soon. :) – WeCanLearnAnything Jul 26 '16 at 3:34
• On closer reading, I actually think that the next step for me is to read the basic documentation on \pgfmath stuff. I need to learn the basic ideas and vocabulary. Is that all in the TikZ & PGF Manual? I found "90.2.6 Pseudo-Random Functions" and "90.3 Pseudo-Random Numbers". Is this where I should start reading? Or is there a less techie/more user-friendly way to – WeCanLearnAnything Jul 26 '16 at 3:46
• @WeCanLearnAnything You may read the part VIII "Mathematical and Object-Oriented Engines" (chapters from 88 to 92) and, may be, "56 Math Library"... – Paul Gaborit Jul 26 '16 at 5:26

Another way to do this:

\documentclass{article}

\usepackage{tikz}

\begin{document}

\foreach \x in {1,...,100}{
\pgfmathparse{int(rand * 11)}
\pgfmathresult, }

\end{document}


Two problems:

• The fact that you repeatedly got the same results is due to a missing seed. As you are dealing with a pseudo random number generator, you will get the same sequence otherwise.

• A quick way to generate random integers between -10 and 10, is to generate ones between 0 and 20 and shift them by -10.

\documentclass{minimal}

\usepackage{tikz}

\begin{document}

\pgfmathsetseed{\number\pdfrandomseed}

\foreach \n in {1,...,100}{

\pgfmathparse{random(0,20)-10}
\pgfmathresult

}

\end{document}