[Numpy-discussion] Numarray: question on RandomArray2.seed(x=0, y=0) system clock default and possible bug

Eric Maryniak e.maryniak at pobox.com
Wed Jul 24 10:29:02 CDT 2002


On Wednesday 24 July 2002 17:59, Chris Barker wrote:
> Just to add my $.02:
>
> I disagree with Eric about what the default behaviour should be. Every
> programming language/environment I have ever used uses some kind of
> "random" seed by default. When I want reproducible results (which I
> often do for testing) I can specify a seed. I find the the most useful
> behaviour. As Eric points out, it is not trivial to generate a "random"
> seed (from the time, or whatever), so it doesn't make sense to burdon
> the nieve user with this chore.
>
> Therefore, I strongly support keeping the default behaviour of a
> "random" seed.

In that case, and if that is the general consensus, RNG should be adapted:
it now uses a fixed seed by default (and not a clock generated one).

> Eric Maryniak wrote:
> > then I'd urge at least to use a better initial seed.
> > In certain applications, e.g. generating session id's in crypto programs,
> > non-predictability of initial seeds is crucial. But if you have a look
> > at GPG's or OpenSSL's source for a PRNG (especially for Windows), it
> > looks like an art in itself. So perhaps RNG's 'clock code' should replace
> > RandomArray2's: it uses microseconds (in gettimeofday), too, and thus
> > will not have the 1-second problem.
>
> This I agree with: a better default initial seed would be great. As
> someone said, "show me the code!". I don't imagine anyone would object
> to improving this.

The source is in Mixranf(), file Numerical/Packages/RNG/Src/ranf.c
(when checked out with CVS), but it may be a good idea to check it
with Python's own random/whrandom code (which I don't have at hand
-- it may be more recent and/or portable for other OSes).

By the way, I realized in my code 'fix' for RandomArray2.seed(x=None,y=None)
that I already anticipated this and that the default behavior is _not_
to use a fixed seed ;-)  :

  if either the x or y seed:

    seed  < 0     ==>  Use the default initial seed value.
    seed  = None  ==>  Set a "random" value for the seed from clock (default)
    seeds >= 0    ==>  Set seed directly (32 bits only).

and en-passant do a better job than clock-based seeding:

---cut---
def seed(x=None,y=None):
    """seed(x, y), set the seed using the integers x, y;
    ...
    """
    if (x != None and type (x) != IntType) or
       (y != None and type (y) != IntType) :
        raise ArgumentError, "seed requires integer arguments (or None)."
    if x == None or y == None:
        # This would be the best, but is problematic under Windows/Mac.
        import dev_random_device  # uses /dev/random or equivalent
        x = dev_random_device.nextvalue()   # egd.sf.net is a user space
        y = dev_random_device.nextvalue()   # alternative
        # So best is to use Mixranf() from RNG/Src/ranf.c here.
    elif x < 0 or y < 0:
        x = 1234567890L
        y = 123456789L
    ranlib.set_seeds(x,y)
---cut---

Bye-bye,

Eric
-- 
Eric Maryniak <e.maryniak at pobox.com>
WWW homepage: http://pobox.com/~e.maryniak/
Mobile phone: +31 6 52047532, or (06) 520 475 32 in NL.

Unix was a trademark of AT&T.
AT&T is a modem test command.




More information about the Numpy-discussion mailing list