[XeTeX] [tex-live] Random number primitives

Joseph Wright joseph.wright at morningstar2.co.uk
Sun Nov 13 10:48:24 CET 2016


On 12/11/2016 23:50, Nelson H. F. Beebe wrote:
> Joseph Wright <joseph.wright at morningstar2.co.uk> writes on
> Sat, 12 Nov 2016 21:45:37 +0000:
> 
>>> Both pdfTeX and LuaTeX include a series of primitives that expose a
>>> lower-level pseudo-random number generator (I assume from C: there is
>>> very little actual code in the pdfTeX WEB source to implement these).
> 
> Since all *TeX engines on Unix(-like) systems these days are built on
> C code originally translated from the Pascal sources, it should be
> possible to supply an interface in all such engines to a C-library
> random-number generator.
> 
> Because the historical rand() is often platform-dependent, and poor,
> I'd recommend the POSIX drand48() family, which is available on all
> systems these days (and I can supply portable code if you feel the
> need for it).  It repairs some of the defects of 32-bit linear
> congruential generators, and should be able to deliver the same stream
> of random numbers from a given starting seed on all platforms.
> 
> It is imperative that users be able to supply an initial seed, because
> otherwise, it is not possible to generate independent streams of
> random numbers of successive runs, such as might be needed for
> multiple simulations.
> 
> However, the default seed should always be a constant, rather than one
> dependent on time, process-id, or other internal data; that way,
> successive runs are reproducible.  Unreproducible output may make
> debugging impossible.
> 
> If you want to improve the quality of the generator beyond what
> drand48() produces, contact me offlist for details of a simple
> extension that costs almost nothing extra, yet dramatically lengthens
> the period and reduces correlations between the output random numbers.

pdfTeX and LuaTeX *already have primitives* that generate random
numbers: I'm no C/WEB/... programmer but from pdftex.web I think it's
likely to be using rand() ultimately. Getting the same result from
pdfTeX and other engines on the same platform is what seems to me to be
important.

Note that the reason for asking about this is to allow pseudo-random
numbers for the type of thing that does make sense from inside a TeX
run. Examples are adding 'interest' in graphics, picking 'm from n
questions' in a list, etc. Being able to set the seed is useful (and
indeed implemented in pdfTeX/LuaTeX), but highly statistically
satisfying randomness is not. (I've never tested the pdf macro-based
implementation, but it's likely to be a balance between having some
'randomness' and being reasonable in TeX macros.)

Joseph



More information about the XeTeX mailing list