[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: feature request: Pre-seeding the random number generator

From: Sean M. Burke
Subject: Re: feature request: Pre-seeding the random number generator
Date: Fri, 27 Oct 2006 00:30:34 -0800
User-agent: Thunderbird (X11/20060922)

On 10/22/2006 03:14 AM, Didier LINK wrote:
Le samedi 21 octobre 2006 à 14:56 -0800, Sean M. Burke a écrit :
This is a feature request: I'd really really appreciate if you could
make the random number generator in ghostscript pre-seeded.

Why do you need this enhancement exactly ?

I like to generate documents that contain, well, some degree of randomness, without having to duck out of PostScript to fetch a new srand seed. For example, I like to reload this:
but I very often get the same two or three mazes over and over and over.

This problem recurs with many other documents of mine that attempt randomness.

If that sounds dull, tell me, and I can make up something totally compelling about how I'm actually the person generating the entire Albanian Lottery's cards using /just/ sh and GhostScript and a can of gold paint, and how the ghost of Hoxha is angry that I keep generating the same four or five cards over and over.
How's that for a use-case and/or a rich comedic premise?

(Perl has been doing this for about ten years-- in the surprisingly
rare circumstances where programmers want /predictable/
pseudorandomness, they just need to explicitly feed srand an particular value.
If you want a bit of inspiration from Perl's pre-seeding, the code for
it is in here:
and look for the function "Perl_seed(pTHX)".)

I considered just replacing rand with something that reads from /dev/random, but besides portability problems, such file operations are apparently inaccessible in gs's (imminently default) SAFER mode.

I think it's possible to introduce a better initial value in the code
with a read from /dev/random but it's not portable on all architectures
that's are supported by ghostscript. I need to investigate in this way.

Excellent! At this point, anything would be better than the current behavior.

(In the old days of my programming in other languages with poor pre-seeding: I used to do pretty well with taking the current process ID number (or, if under Windows, instead using GetTickCount, since Windows often reuses process IDs almost immediately), shift it four bits left, and xor that against the lower twelve bits of time or utime or something of the sort.)

I used to pre-seed by just doing "realtime srand", but now I see that
realtime has a very NONrandom value (see below).

Yes I see, and it's plateform dependent, on my amd64 the random numbers
generated by your perl script are a little bit better. But why do a
srand at each iteration of the loop ? It's certainly better to
initialize one time and use rrand function to iterate new random numbers
inside gs. I'm wrong ?

It was just minimal example code to illustrate the problem of realtime's value not being useful as an initial value for srand.

I know that PostScript docs typically warn that rand's generator is typically pretty dumb, but that doesn't mean it /has/ to be dumb in GhostScript.
Yes but they need to be compliant with the Adobe's specifications and
reference implementation of postscript interpreter.

I appreciate you noting that One Must Be Very Careful-- but I swear, if Adobe actually does mandate that the random number generator /not/ be pre-seeded, I will SHOWER you with gifts.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]