4

I've found two answers for ``How to get a new PGF seed each compilation?'' 1 2, and they work wonderfully on PdfLaTeX.

However, neither of the methods work on XeLaTeX. I get the error

! Undefined control sequence.
<argument> \number \pdfrandomseed
l.6 \pgfmathsetseed{\number\pdfrandomseed}

when using the first method, and

! Undefined control sequence.
<argument> \pdfuniformdeviate
10000000
l.7 \pgfmathsetseed{\pdfuniformdeviate 10000000}

when using the second. How can these methods be fixed for XeLaTeX use? Is there a particular method for getting a random seed each time in XeLaTeX?

MWE

\documentclass{article}
\usepackage{tikz}

% Two ways to get a new seed every compilation \pgfmathsetseed{\number\pdfrandomseed} % https://tex.stackexchange.com/a/144623 %\pgfmathsetseed{\pdfuniformdeviate 10000000} % http://tex.stackexchange.com/questions/212738/making-a-random-number

\pgfmathsetmacro\numr{rand}% Print a random number

\begin{document}

\numr

\end{document}

Imp54
  • 83
  • 6
  • 1
    In XeTeX it doesn't have the pdf prefix. It's \randomseed – Phelype Oleinik Dec 20 '20 at 19:53
  • @PhelypeOleinik Agh. Just now I realize the first method's author said With XeLaTeX there's nothing similar. But I tried just using \randomseed and it didn't work either. The error changes but the random number still defaults to -1.0, just as before. – Imp54 Dec 20 '20 at 19:58
  • 1
    Ah, \randomseed was added to XeTeX on 2019, so you need a failrly recent system. – Phelype Oleinik Dec 20 '20 at 19:59
  • I see. Thanks. I'll try updating and come back. – Imp54 Dec 20 '20 at 20:02

1 Answers1

7

pdfTeX adopted the pdf prefix for most (all?) primitives it introduced, so they are called \pdf<name> even if they have nothing to do with PDF output. In XeTeX these primitives were added in 2019, but they are called only \<name>, so your code becomes (both \randomseed and \uniformdeviate work):

\documentclass{article}
\usepackage{tikz}

% Two ways to get a new seed every compilation \pgfmathsetseed{\number\randomseed} % https://tex.stackexchange.com/a/144623 % \pgfmathsetseed{\uniformdeviate 10000000} % http://tex.stackexchange.com/questions/212738/making-a-random-number

\pgfmathsetmacro\numr{rand}% Print a random number

\begin{document}

\numr

\end{document}

If you want engine independence, you can use expl3's \sys_rand_seed:. It will work on all engines supported by expl3 (pdfTeX, XeTeX, LuaTex, epTeX and eupTeX):

\documentclass{article}
\usepackage{tikz}
\usepackage{expl3}

\ExplSyntaxOn \cs_new_eq:NN \RandomSeed \sys_rand_seed: \ExplSyntaxOff

% Two ways to get a new seed every compilation \pgfmathsetseed{\RandomSeed} % https://tex.stackexchange.com/a/144623 % \pgfmathsetseed{\uniformdeviate 10000000} % http://tex.stackexchange.com/questions/212738/making-a-random-number

\pgfmathsetmacro\numr{rand}% Print a random number

\begin{document}

\numr

\end{document}

  • I tried updating my texlive with the commands shown here, (changing 2017 for 2020) but I still get an error when uncommenting either of the \pgfmathsetseed lines (expl3 part works well).

    I realize this it outside the scope of the question. Should I ask this in the chat, perhaps?

    – Imp54 Dec 21 '20 at 01:57
  • Indeed it is strange that \RandomSeed is defined without issue, but its call throws an error, ! Undefined control sequence. <argument> \RandomSeed l.9 \pgfmathsetseed{\RandomSeed}

    Maybe my problem is with expl3 and not with my entire texlive?

    – Imp54 Dec 21 '20 at 02:04
  • 1
    @Imp54 As a former user of the TeX Live packaged for Ubuntu, I strongly suggest you install the vanilla version from tug.org (https://tug.org/texlive/acquire-netinstall.html: download the install-tl-unx.tar.gz file). When you use \cs_new_eq:NN (which is a fancy \let), TeX always makes a copy of the control sequence, even if it is undefined. Then, when you try to use it, you get an error because the copy is undefined too. It seems that the update didn't quite work. What does tex --version say? – Phelype Oleinik Dec 21 '20 at 02:39
  • Thanks for explaining. I know very little about expl3. 2) tex --version gives `TeX 3.14159265 (TeX Live 2015/Debian)
  • kpathsea version 6.2.1 I am using Ubuntu 16.04, but my target distribution was 2020 (I thought tweaking the method from the AskUbuntu page was enough). Oddly enough, my/usr/local/texlive/2020/release-texlive.txtsaysversion 2020`

    – Imp54 Dec 21 '20 at 02:53
  • 1
    @Imp54 Okay, so the version you get from the terminal is 2015 (rather old, but it's what you get packaged with Ubuntu 16.04: I'm on 16.04 too). Though it seems you do have TL 2020 installed, so maybe it's a question of setting the path correctly. Try adding export PATH="/usr/local/texlive/2020/bin/x86_64-linux/":$PATH to your ~/.bashrc file (double-check if the binaries are in that folder, but I think that's the right one). – Phelype Oleinik Dec 21 '20 at 03:05
  • All done. All three methods work now. I'm ectasic even though I learned I really should update my Ubuntu distro :( – Imp54 Dec 21 '20 at 03:22
  • 1
    @Imp54 Good to know it's working! Off-topic to the question: I'm clinging to my 16.04 because I really dislike the GNOME interface in newer releases. I'm considering Arch or Fedora, when I get to reset my system. In any case, I no longer use the pre-packaged version of TeXLive: the vanilla one is really easy to install and to update, and it is also easy to have multiple versions installed (I have 2015, 2018, 2019, and 2020 here). – Phelype Oleinik Dec 21 '20 at 03:33
  • I'm holding off for the same reason. Thanks for answering all of my questions :) – Imp54 Dec 21 '20 at 03:52