This "answer" does not provide a solution to the problem.
As in your question the lcg-package is used, let's mention that the lcg package (The lcg package, 2013/08/09 (v1.3), CTAN: https://www.ctan.org/pkg/lcg) produces spurious space tokens.
(I already tried to contact the package author about this via the e-mail-address provided in the package documentation. Neither did the mail bounce nor did I get any reaction. We live in turbulent times. It might be good to put these things in order. But I don't want to hijack/take over the package, because that would probably be unpolite.)
You can easily reproduce this phenomenon:
\documentclass{article}
\usepackage{lcg}
\begin{document}
\noindent X\rand X
\noindent X\chgrand[first=1,last=20,quiet=y]X
\noindent X\rand X
\end{document}
\rand and \chgrand and the like should not produce visible output.
Thus I expected the visual output of the minimal example above to be something like this:
XX
XX
XX
But the X are interspersed with spaces and you get something like this:
X X
X X
XX
The reason is that both the definition-text of the key quiet and the definition-text of the macro \r@nd produce spurious space tokens.
In the following I copied these definitions and marked with <-!!!!! the places where the spurious space tokens come into being:
84 \define@key{Init}{quiet}[y]{ <-!!!!!
85 \def\qui@t{\expandafter\firstletterr@nd #1\delimiter} <-!!!!!
86 \if \qui@t y% nothing to do
87 \else\if\qui@t Y \def\qui@t{y} <-!!!!! (2x)
88 \else\if\qui@t j \def\qui@t{y} <-!!!!! (2x)
89 \else\if\qui@t J \def\qui@t{y} <-!!!!! (2x)
90 \else\if\qui@t n \def\qui@t{n} <-!!!!! (2x)
91 \else\if\qui@t N \def\qui@t{n} <-!!!!! (2x)
92 \else
93 \PackageWarning{lcg}{Value of key <quiet> must be <y> or <n>} <-!!!!!
94 \def\qui@t{y} <-!!!!!
95 \fi\fi\fi\fi\fi\fi
96 }
173 \def\r@nd{%
174 \ifnum \cr@nd < @ne% then ... initialize generator
175 \cr@nd = \the\time
176 \advance \cr@nd \inputlineno
177 \multiply \cr@nd \value{page} <-!!!!!
178 \advance \cr@nd \the\year
179 \multiply \cr@nd \the\month
180 \multiply \cr@nd \the\day
181 \advance \cr@nd \inputlineno
182 \if \qui@t y%
183 \else
184 \typeout{Random number generator initialized to \the\cr@nd}%
185 \fi
186 \r@nd%
187 \else % else ... generate new number
188 @tempcnta = \cr@nd
189 \divide @tempcnta 127773 % @tempcnta = floor(z/q)
190 @tempcntb = @tempcnta % @tempcntb = floor(z/q)
191 \multiply @tempcnta -2836 % @tempcnta = -rfloor(z/q)
192 \multiply @tempcntb -127773 % @tempcntb = -qfloor(z/q)
193 \advance \cr@nd @tempcntb % cr@nd = z mod q
194 \multiply \cr@nd 16807 % cr@nd = a * (z mod q)
195 \advance \cr@nd @tempcnta % cr@nd = az mod m
196 \ifnum \cr@nd < \z@%
197 \advance \cr@nd 2147483647 % cr@nd = (az mod m) > 0
198 \fi
199 \global\cr@nd=\cr@nd % persist the change outside current scope
200 \fi
201 }% end of \r@nd
I suggest the following changes:
\define@key{Init}{quiet}[y]{%
\def\qui@t{\expandafter\firstletterr@nd #1\delimiter}%
\if \qui@t y% nothing to do
\else\if\qui@t Y\def\qui@t{y}%
\else\if\qui@t j\def\qui@t{y}%
\else\if\qui@t J\def\qui@t{y}%
\else\if\qui@t n\def\qui@t{n}%
\else\if\qui@t N\def\qui@t{n}%
\else
\PackageWarning{lcg}{Value of key <quiet> must be <y> or <n>}%
\def\qui@t{y}%
\fi\fi\fi\fi\fi\fi
}
\def\rand{%
\r@nd%
\@tempcnta
\@tempcntb
\@tempcnta = \f@rst
\@tempcntb = \l@st
\multiply \@tempcnta \m@ne
\advance \@tempcntb \@tempcnta
\advance \@tempcntb \@ne %l@st-f@rst+1
\@tempcnta = 2147483647
\divide \@tempcnta \@tempcntb
\multiply \@tempcnta \@tempcntb
\ifnum \cr@nd > \@tempcnta\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
{\rand}%
{%
\setcounter{\r@ndcountername}{\cr@nd}%
\@tempcnta = \cr@nd
\divide \@tempcnta \@tempcntb
\multiply \@tempcnta \@tempcntb
\multiply \@tempcnta \m@ne
\addtocounter{\r@ndcountername}{\@tempcnta}%
\addtocounter{\r@ndcountername}{\f@rst}%
}%
}% end of \rand
\def\r@nd{%
\ifnum \cr@nd < \@ne\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
{% then ... initialize generator
\cr@nd = \the\time
\advance \cr@nd \inputlineno
\multiply \cr@nd \value{page}%%%%%%%
\advance \cr@nd \the\year
\multiply \cr@nd \the\month
\multiply \cr@nd \the\day
\advance \cr@nd \inputlineno
\if \qui@t y%
\else
\typeout{Random number generator initialized to \the\cr@nd}%
\fi
\r@nd%
}{% else ... generate new number
\@tempcnta = \cr@nd
\divide \@tempcnta 127773 % \@tempcnta = floor(z/q)
\@tempcntb = \@tempcnta % \@tempcntb = floor(z/q)
\multiply \@tempcnta -2836 % \@tempcnta = -r*floor(z/q)
\multiply \@tempcntb -127773 % \@tempcntb = -q*floor(z/q)
\advance \cr@nd \@tempcntb % cr@nd = z mod q
\multiply \cr@nd 16807 % cr@nd = a * (z mod q)
\advance \cr@nd \@tempcnta % cr@nd = a*z mod m
\ifnum \cr@nd < \z@%
\advance \cr@nd 2147483647 % cr@nd = (a*z mod m) > 0
\fi
\global\cr@nd=\cr@nd % persist the change outside current scope
}%
}% end of \r@nd
\ExplSyntaxOn\newcommand{\firstpower}{\int_rand:nn{2}{5}}\ExplSyntaxOffthat is an expandable number. – Ulrike Fischer Oct 12 '20 at 16:47