7

Right now, I am generating a random color in pdftex using

\definecolor{randomcolor}{RGB}
{
    \pdfuniformdeviate 255,
    \pdfuniformdeviate 255,
    \pdfuniformdeviate 255
}

Which works pretty well, until I try and compile with luatex.

What is the nicest way to generate random colors (either directly or via generating 3 random numbers), that is compatible across both pdftex and luatex?

I suspect something involving pgf is the answer. Or maybe just a condition with one branch for pdftex (that looks like the current), and one for luatex (that maybe invokes a tiny snippet of luacode).

2
  • 2
    Is the luatex85 package usable? It brings back all the deleted \pdf... macros to lualatex
    – daleif
    Commented Aug 21, 2018 at 11:06
  • @daleif yes. Expand that into an answer, with a link to the docs maybe and that would be great. Assuming luatex85 is a no-op or something in pdftex Commented Aug 21, 2018 at 11:14

3 Answers 3

9

The luatex85 package does

\let\pdfuniformdeviate\uniformdeviate

You can thus do \usepackage{luatex85} or

\documentclass{article}
\usepackage{xcolor}

\unless\ifdefined\pdfuniformdeviate
  \let\pdfuniformdeviate\uniformdeviate
\fi

\definecolor{randomcolor}{RGB}
{
    \pdfuniformdeviate 255,
    \pdfuniformdeviate 255,
    \pdfuniformdeviate 255
}
\extractcolorspec{randomcolor}\test
\typeout{\test}

\stop

This will type out something like

{rgb}{0.84706,0.4,0.68234}

with both pdflatex or lualatex. I guess that, when random numbers will be available also in xelatex, a common interface will be added to the LaTeX kernel.

4

Based on Randomly assign background colour for each frame

Another approach based on pgf:

\documentclass{article}

\usepackage{pgf}
\usepackage{pgffor}
\usepackage{xcolor}

\usepackage{ifluatex}
\usepackage{ifxetex}

\ifluatex
  \let\pdfrandomseed\randomseed
\fi

\ifxetex
  \pgfmathsetseed{\time}
\else
  \pgfmathsetseed{\number\pdfrandomseed}
\fi

\newcommand{\randomcolor}{%
    \pgfmathsetmacro{\R}{rnd}%
    \pgfmathsetmacro{\G}{rnd}%
    \pgfmathsetmacro{\B}{rnd}%
    \definecolor{randomcol}{rgb}{\R,\G,\B}%
}

\begin{document}

\noindent%
\foreach \n in {1,...,1350}{%
   \randomcolor\color{randomcol}\rule{0.41cm}{0.41cm}\-%
}

\end{document}

enter image description here

4

There is also the lcg package (short for Linear Congruential Generator), that works with all compilers, including lualatex. The package provides a command \rand to generate a random number and store it in the rand counter. For use in \definecolor you should convert the counter with \the\value.

You can set the bounds of the random generator with the package options first and last, and optionally change them within the document using the \reinitrand command. The default seed is based on the system clock, using the minutes as unit (so the output will change only once per minute). You can also provide a custom seed.

MWE:

\documentclass{article}
\usepackage[first=1,last=255]{lcg}
\usepackage{xcolor}
\rand\edef\colora{\the\value{rand}}
\rand\edef\colorb{\the\value{rand}}
\rand\edef\colorc{\the\value{rand}}

\definecolor{randomcolor}{RGB}
{
    \colora,
    \colorb,
    \colorc
}
\begin{document}
\colorbox{randomcolor}{Random color: \colora,\colorb,\colorc}
\end{document}

Result:

enter image description here

2
  • 1
    Use \the\value{rand} instead of \arabic. \arabic is a formatting command and there is no garanty that it gives a number (e.g. hebrew and arabic packages often redefine it). Commented Aug 21, 2018 at 19:08
  • @UlrikeFischer thanks for the improvement, it didn't occur to me to put \the there - edited.
    – Marijn
    Commented Aug 23, 2018 at 11:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .