3

myRandomInteger works fine under package pgfmath. If package pgfplots is also loaded compilation error !Package PGF Math Error: Sorry, the operation 'random' has not yet been implemented in the floating point unit (in 'random(1,10000)'). ... occurs. I would like to use the random functions of pgfmath with pgfplots also available. Any luck there?

\documentclass{article}
%  RN. 23 November 2016
%  DESCRIPTION: clash of pgfmath and pgfplots packages.
\usepackage{xparse} 
\usepackage{pgfmath}
%\usepackage{pgfplots} 
%\pgfplotsset{compat=1.13} 
\ExplSyntaxOn
\cs_new:Npn \rn_RandomInteger:nn #1#2
  {
    \pgfmathparse{random(#1,#2)}
  }
\NewDocumentCommand\myRandomInteger{O{1}O{100}}
  {
    \rn_RandomInteger:nn {#1}{#2}
    \pgfmathresult
  }
\ExplSyntaxOff
\begin{document}
    \myRandomInteger,
    \myRandomInteger, 
    \myRandomInteger

    \myRandomInteger[1][10000],
    \myRandomInteger[1][10000],
    \myRandomInteger[1][10000]

    \myRandomInteger[1][5],
    \myRandomInteger[1][5],
    \myRandomInteger[1][5]
\end{document}
3

It is due to a bug in the fpu library as explained in Christian Feuersänger's answer. Modifying the workaround supplied there for the function you require, we can write the following to get things working.

\documentclass{article}
\usepackage{xparse}
\usepackage{pgfmath}
% adapted from ateb Christian Feuersänger: https://tex.stackexchange.com/a/330076/
\makeatletter
\let\pgfmathrandomX=\pgfmathrandom@
\usepackage{pgfplots}
\let\pgfmathrandom@=\pgfmathrandomX
\makeatother
\ExplSyntaxOn
\cs_new:Npn \rn_RandomInteger:nn #1#2
  {
    \pgfmathparse{random(#1,#2)}
  }
\NewDocumentCommand\myRandomInteger{O{1}O{100}}
  {
    \rn_RandomInteger:nn {#1}{#2}
    \pgfmathresult
  }
\ExplSyntaxOff
\begin{document}
    \myRandomInteger,
    \myRandomInteger,
    \myRandomInteger

    \myRandomInteger[1][10000],
    \myRandomInteger[1][10000],
    \myRandomInteger[1][10000]

    \myRandomInteger[1][5],
    \myRandomInteger[1][5],
    \myRandomInteger[1][5]
\end{document}
2
  • Problem solved, thanks! I normally keep all my \usepackage statements in an .sty file for better housekeeping, but know from experience that \makeatletter ... \makeatother constructs cause trouble in .sty files, so I better keep this in the preamble of my documents if required. Do you have a view on this? Nov 23 '16 at 9:12
  • @ReinhardNeuwirth If you put the code in a .sty, just remove \makeatletter and \makeatother. They are only needed because the category code of @ is different in a document than in .cls and .sty files. If you are in a .cls or .sty file, then the code is already correct.
    – cfr
    Nov 23 '16 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.