From: Eric M. <e.m...@po...> - 2002-07-24 17:28:14
|
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 progr= ams, > > non-predictability of initial seeds is crucial. But if you have a loo= k > > 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 rep= lace > > 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=3DNone,y=3D= 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 =3D=3D> Use the default initial seed value. seed =3D None =3D=3D> Set a "random" value for the seed from clock= (default) seeds >=3D 0 =3D=3D> Set seed directly (32 bits only). and en-passant do a better job than clock-based seeding: ---cut--- def seed(x=3DNone,y=3DNone): """seed(x, y), set the seed using the integers x, y; ... """ if (x !=3D None and type (x) !=3D IntType) or (y !=3D None and type (y) !=3D IntType) : raise ArgumentError, "seed requires integer arguments (or None)." if x =3D=3D None or y =3D=3D None: # This would be the best, but is problematic under Windows/Mac. import dev_random_device # uses /dev/random or equivalent x =3D dev_random_device.nextvalue() # egd.sf.net is a user spac= e y =3D dev_random_device.nextvalue() # alternative # So best is to use Mixranf() from RNG/Src/ranf.c here. elif x < 0 or y < 0: x =3D 1234567890L y =3D 123456789L ranlib.set_seeds(x,y) ---cut--- Bye-bye, Eric --=20 Eric Maryniak <e.m...@po...> 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. |