Pick Randomly Between -1 or 1

There is a PGF math function to choose randomly from a list of items. This can be used to solve the general problem:

\documentclass{article}
\usepackage{pgffor}
\begin{document}
\pgfmathdeclarerandomlist{ones}{{-1}{1}}
\foreach \x in {1,...,10}{
\pgfmathrandomitem{\choice}{ones}
\choice\space
}

\pgfmathdeclarerandomlist{choices}{{1}{0}{-1}{2}{-2}}
\foreach\x in {1,...,10}{
\pgfmathrandomitem{\choice}{choices}
\choice\space
}
\end{document}

output of code


Here's a LuaLaTeX-based solution. It outputs the result of \x in math mode; if you don't need that, just remove the two $ symbols used in the definition of \x. (In case you're curious how this works: The Lua function math.random(2) generates the numbers 1 and 2 with equal probability. Multiplying math.random(2) by 2 and subtracting 3 thus generates the numbers -1 and 1 with equal probability.)

enter image description here

\documentclass{article}
\newcommand\x{$\directlua{tex.sprint(2*math.random(2)-3)}$}
\begin{document}
\x, \x, \x, \x, \x; \x, \x, \x, \x, \x; \x, \x, \x, \x, \x  
\end{document}

You further asked,

How would I randomly assign \x a value from a fixed and arbitrary set of constants?

I will assume that these constants can be either numbers or strings. It's straightforward to generalize the Lua-based approach shown above to handle this case. First, use a \luaexec directive to set up a Lua table -- not to be confused with a LaTeX table! -- that contains N constants -- basically, an Nx1 list of comma-separated numbers and strings; in the code below, N=5. (It is possible to have LaTeX macros among the string constants. Do observe, though, that it's necessary to "escape" backslash (\) characters, i.e., to input them as \\.) Second, set up a LaTeX macro -- named \x, as before -- that picks off 1 of the N entries using a math.random(N) call.

enter image description here

\documentclass{article}
\usepackage{luacode} % for "\luaexec" macro

% Define a Lua table that contains N (fairly) arbitrary entries
\luaexec{N = 5 ;
         mylist = {5, 
                   "abc", 
                   "DEF", 
                   "$\\Gamma$", 
                   "$\\frac{\\pi^2}{\\int_0^1\\!f(x)\\,dx}$" 
         } }

% Define a LaTeX macro that selects one of the N entries at random
\newcommand\x{\directlua{tex.sprint(mylist[math.random(N)])}}

\begin{document}
\obeylines % just for this example
\x, \x, \x, \x, \x
\x, \x, \x, \x, \x
\x, \x, \x, \x, \x  
\end{document}

The apparent choice from -1,0, and 1 is due to a bug and random shouldn't be used with negative arguments as given in \pgfmathparse returns a phantom .0.0

Instead you can use the macro in the designated domain and then shift/scale.

simplest is perhaps

\pgfmathsetmacro{\x}{int(2*random(0,1)-1)}