How to Obtain a Random Non-Zero Integer?
Choose a random number between 1 and X (thereby avoiding 0) and multiply it with 1 or -1 based on a uniform distribution (thereby obtaining a sign):
\documentclass{article}
\usepackage{tikz}
\pgfmathsetseed{\number\pdfrandomseed}
\newcommand{\InitVariables}{%
\pgfmathsetmacro{\a}{int(ifthenelse(rand > 0, 1, -1)*random(1,5))}%
}
\begin{document}
\foreach \x in {1,...,100}{\InitVariables$\a$ }
\end{document}
The first calculation only chooses the number of random values that are needed. The inclusive range [-5, 5] without 0 has 10 values.
\pgfmathsetmacro{\a}{random(0,9)-5}% range [-5, 4]
Then the negative values are in the correct range. The non-negative values are increased by one to move the range [0, 4] to [1, 5].
\pgfmathsetmacro{\a}{int(ifthenelse(\a<0, \a, \a + 1)}%
Full example:
\documentclass{article}
\usepackage{pgf}
\usepackage{pgffor}
\pgfmathsetseed{\number\pdfrandomseed}
\newcommand{\InitVariables}{%
\pgfmathsetmacro{\a}{random(0,9)-5}%
\pgfmathsetmacro{\a}{int(ifthenelse(\a<0, \a, \a + 1)}%
}
\begin{document}
\noindent
\foreach \i in {0, ..., 200} {%
\InitVariables
\a\space
}
\end{document}
Version with the minimum and maximum values as macros:
\documentclass{article}
\usepackage{pgf}
\usepackage{pgffor}
\pgfmathsetseed{\number\pdfrandomseed}
\newcommand*{\RandomMinimum}{-300}
\newcommand*{\RandomMaximum}{700}
\newcommand*{\InitVariables}{%
\pgfmathsetmacro{\a}{%
\RandomMinimum + random(0, int(\RandomMaximum - int(\RandomMinimum)))
}%
\pgfmathtruncatemacro{\a}{ifthenelse(\a<0, \a, \a + 1}%
}
\begin{document}
\noindent
\foreach \i in {0, ..., 200} {%
\InitVariables
\a\space
}
\end{document}
Generate a number between 0 and 2x –1, then normalize it: if it is less than x, subtract x, otherwise subtract x and add 1.
\documentclass{article}
\newcommand{\randomdef}[2]{%
\edef#1{%
\expandafter\randomdefnormalize\pdfuniformdeviate\numexpr#2*2\relax\foo{#2}%
}%
}
\def\randomdefnormalize#1\foo#2{%
\ifnum#1<#2
\the\numexpr#1-#2\relax
\else
\the\numexpr#1-#2+1\relax
\fi
}
\begin{document}
\randomdef{\arandom}{5}$\arandom$,\space\randomdef{\arandom}{5}$\arandom$,\space
\randomdef{\arandom}{5}$\arandom$,\space\randomdef{\arandom}{5}$\arandom$,\space
\randomdef{\arandom}{5}$\arandom$,\space\randomdef{\arandom}{5}$\arandom$,\space
\randomdef{\arandom}{5}$\arandom$,\space\randomdef{\arandom}{5}$\arandom$,\space
\randomdef{\arandom}{5}$\arandom$,\space\randomdef{\arandom}{5}$\arandom$,\space
\randomdef{\arandom}{5}$\arandom$,\space\randomdef{\arandom}{5}$\arandom$,\space
\randomdef{\arandom}{5}$\arandom$,\space\randomdef{\arandom}{5}$\arandom$,\space
\randomdef{\arandom}{5}$\arandom$,\space\randomdef{\arandom}{5}$\arandom$
\end{document}
The same can of course be done with PGF features.
With xparse
and expl3
; also an interface to expandably get the number in the required interval. Here I use a different strategy: the random number is generated in the interval –x, x –1; if it's positive, I add 1.
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\randomdef}{mm}
{
\wcla_random_def:Nn #1 { #2 }
}
\DeclareExpandableDocumentCommand{\randomget}{m}
{
\wcla_random_get:n { #1 }
}
\cs_new_protected:Nn \wcla_random_def:Nn
{
\cs_set:Npx #1 { \wcla_random_get:n { #2 } }
}
\cs_new:Nn \wcla_random_get:n
{
\__wcla_random_get:f { \fp_eval:n { randint(-#1,#1-1) } }
}
\cs_new:Nn \__wcla_random_get:n
{
\int_compare:nTF { #1 < 0 } { #1 } { \fp_eval:n { #1+1 } }
}
\cs_generate_variant:Nn \__wcla_random_get:n { f }
\ExplSyntaxOff
\begin{document}
\randomdef{\arandom}{5}$\arandom$,\space\randomdef{\arandom}{5}$\arandom$,\space
\randomdef{\arandom}{5}$\arandom$,\space\randomdef{\arandom}{5}$\arandom$,\space
\randomdef{\arandom}{5}$\arandom$,\space\randomdef{\arandom}{5}$\arandom$,\space
\randomdef{\arandom}{5}$\arandom$,\space\randomdef{\arandom}{5}$\arandom$,\space
\randomdef{\arandom}{5}$\arandom$,\space\randomdef{\arandom}{5}$\arandom$,\space
\randomdef{\arandom}{5}$\arandom$,\space\randomdef{\arandom}{5}$\arandom$,\space
\randomdef{\arandom}{5}$\arandom$,\space\randomdef{\arandom}{5}$\arandom$,\space
\randomdef{\arandom}{5}$\arandom$,\space\randomdef{\arandom}{5}$\arandom$
$\randomget{5}$, $\randomget{5}$, $\randomget{5}$, $\randomget{5}$,
$\randomget{5}$, $\randomget{5}$, $\randomget{5}$, $\randomget{5}$,
$\randomget{5}$, $\randomget{5}$, $\randomget{5}$, $\randomget{5}$,
$\randomget{5}$, $\randomget{5}$, $\randomget{5}$, $\randomget{5}$
\end{document}