# How to define a random number with xcolor and tikz/pgf

I tried to get a random color with xcolor and tikz:

\documentclass{article}
\usepackage[xetex, rgb]{xcolor}
\usepackage{tikz}

\begin{document}

\pgfmathsetseed{1}
\begin{tikzpicture}
\xdefinecolor{MyColor}{hsb}{rand, 1.0, 1.0}
\node[fill=MyColor]{Hello};
\end{tikzpicture}
\end{document}

But this does not work, it reports the following error

Missing number, treated as zero

Does anybody know how to get random colors? thanks.

-

\begin{tikzpicture}
\pgfmathparse{rnd}
\xdefinecolor{MyColor}{hsb}{\pgfmathresult, 1.0, 1.0}
\node[fill=MyColor]{Hello};
\end{tikzpicture}
-
I get ERROR: Package pgfbase Error: Unsupported color model 'hsb'. Sorry.. – Georges Dupéron Sep 4 '13 at 10:33
@GeorgesDupéron The xcolor package is required – egreg Sep 4 '13 at 12:17
Yes, I found out that one needs \usepackage[rgb]{xcolor} for most documentclass, but that doesn't work with beamer, since it already loads xcolor with other options, so one needs to \documentclass[rgb]{beamer} instead. – Georges Dupéron Sep 4 '13 at 12:30
@GeorgesDupéron The recommended way should be \documentclass[xcolor={rgb}]{beamer} – egreg Sep 4 '13 at 12:31

The egre solution gives you a Pseudorandom color.

If you use pdftex or one of his son, I suggest you this solution, where for every compilation you will obtain a different color.

\documentclass{standalone}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\pgfmathparse{rnd}
\xdefinecolor{MyColor}{rgb}{\pgfmathresult, 1.0, 1.0}
\node[fill=MyColor](A) at (0,0){Hello};

\edef\R{\pdfuniformdeviate 255}
\edef\G{\pdfuniformdeviate 255}
\edef\B{\pdfuniformdeviate 255}
\xdefinecolor{MyColor2}{RGB}{\R,\G,\B}
\node[fill=MyColor2](A2) at (0,-.5){Hello};
\end{tikzpicture}
\end{document}
-