1

Metapost allows you to set a seed for the random number generator, so that you can get a predictable sequence of numbers from uniformdeviate and normaldeviate.

I can set it like this: randomseed := 1234; just like any other parameter or variable. But when I try to query the value, with a simple program like this:

show randomseed;
end.

I get an this error:

! An expression can't begin with `randomseed'.

Is there some other way to query the current value of randomseed?

Thruston
  • 42,268
  • randomseed is a <command>, not a variable. Page 95 of mpman.pdf (texdoc metapost). Sorry, but I don't have the Metafontbook at hand for checking there. – egreg Aug 20 '15 at 22:40
  • I've now checked back with my own copy of the Metafont book and yes it makes it clear that it's a command. This raises the question of why is needs := after it as if it was a variable, but I guess that's a TeX history question. – Thruston Aug 21 '15 at 14:27
  • It's indeed quite strange, but that's what it is. – egreg Aug 21 '15 at 14:31
  • More helpfully I see that the MFbook provides the useful information that the default value is day+time*epsilon -- this might provide the basis of an answer. – Thruston Aug 21 '15 at 15:37

1 Answers1

5

Apologies for answering my own question, but I thought I'd better record what I found.

Short answer: no, you can't query the default value of randomseed.

Longer answer and a hack to work round the problem.

  1. Despite the fact that you have to write randomseed := 1234; in a way that looks like an assignment, Metapost defines this as a command and not an assignment, and does not provide any way to query the current value. There is just no user-land variable name provided for the random seed. The assignment-like syntax is apparently just a historical quirk.

  2. Contrary to what is says in the Metafontbook, the default value in the MP library is not day+time*epsilon but rather time/1+day. However this default is overridden at the start of the program for the command line program mpost.
    The code that does this around line 520 of mpost.w and contains three alternative sections depending on which system time functions are available. In summary the value is set from the current time of day at the highest resolution available on the local system. So it's different every time you run MP and rather hard to predict.

  3. Here's a hack that helped me: set your own random seed using a random number.

    If you add the following before you produce a diagram that depends on random numbers

    randomseed := uniformdeviate infinity;
    

    then you will get a different random seed every time, but because you called the randomseed function, the value used will be written in the log file, like this:

    {randomseed:=2772.6951}
    

    So you can run MP several times until your random diagram looks just right, and then you can copy the value of the random seed from the log back into your program so that you can re-create it exactly.

    Note that if you are using lualatex and the luamplib package to produce your MP diagrams, the contents of the MP log will only be included in the tex log if you add \mplibshowlog{enable} to your pre-amble.

Thruston
  • 42,268