timepostscriptrandom-seedmillisecondssystem-clock

How do I get a large integer value from the REALTIME operator in postscript? miliseconds?


The REALTIME operator is supposed to output the time in miliseconds. I want to use that as a seed for RAND, but the output is always a two digit number between 24 and 31. How am I misunderstanding the realtime operator? Is there an alternative I cannot find in the red,green,blue reference books? I am using Linux (ubuntu 22) Ghostscript gs --version 9.55.0

CODE

%!PS
/inch {72 mul} def
/Times-Roman findfont 15 scalefont setfont
1 inch 10 inch moveto
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

/str6 200 string def
/rrr {realtime} def
rrr 100000 mul  str6 cvs  show
(   ) show

/str6 200 string def
/rrr {realtime} def
rrr 100000 mul  str6 cvs  show
(   ) show

/str6 200 string def
/rrr {realtime} def
rrr 100000 mul  str6 cvs  show
(   ) show

/str6 200 string def
/rrr {realtime} def
rrr 100000 mul  str6 cvs  show
(   ) show

/str6 200 string def
/rrr {realtime} def
rrr 100000 mul  str6 cvs  show
(   ) show

/str6 200 string def
/rrr {realtime} def
rrr 100000 mul  str6 cvs  show
(   ) show

showpage

%   OUTPUT IS ALWAYS A 2 DIGIT INT WITH 5 NOUGHTS
%
%   2700000  2700000  2700000  2700000  2700000  2700000  
%

Solution

  • Using a binary built from current HEAD of the Git repository, the realtime operator does not return a value between 24 and 31 for me, I get a steadily increasing value.

    It is possible that the Ubuntu package maintainers have altered the action of realtime, or that something has changed in the last 3 years. Try building from current source, you will need a C compiler (gcc or clang) GNU make and autotools.

    Instead of using cvs and show to print the value on the output, try realtime == which will send the result to the back channel. You can do this from the interactive PostScript prompt.

    You don't need to keep defining strings, you should use the stack instead. In fact you don't need to keep defining key/value pairs in userdict as you are doing.

    For example your code:

    /str6 200 string def
    /rrr {realtime} def
    rrr 100000 mul  str6 cvs  show
    (   ) show
    

    Could be written as:

    realtime
    100000 mul
    100 string cvs
    show
    

    You could write this as a function:

    /print_realtime
    {
        realtime
        100000 mul
        100 string cvs
        show
    } bind def
    

    All done without defining str6 or rrr at all, let alone redefining them constantly.

    Multiplying by 100000 is going to run out of integer values really quickly on most PostScript interpreters.

    Note that the PLRM says:

    "The unit of time represented by the realtime value is one millisecond. However, the rate at which it changes is implementation-dependent."

    So technically it need not change at all. You might be better to use usertime, though that may only track the processor execution time.

    If I remember correctly Ghostscript on Linux uses gettimeofday for both functions.