More on Random
David N. Smith (IBM)
dnsmith at watson.ibm.com
Thu Jun 10 17:39:24 UTC 1999
Hi:
I suspect that the problem is that Time millisecondClockValue times the
hash value is answering a value greater than m (16r7FFFFFFF). I'd suggest
the following code instead. It guarantees that the self seed value is never
greater than m/2, so the test in #nextValue should never produce a negative
value after adding m. (If the mask is 16r7FFFFFFF, it is possible to get an
answer of 1.0 when clock*hash has all ones.)
initialize
" Set a reasonable Park-Miller starting seed "
seed := Time millisecondClockValue bitAnd: 16r3FFFFFFF.
a := 16r000041A7 asFloat. " magic constant = 16807 "
m := 16r7FFFFFFF asFloat. " magic constant = 2147483647 "
q := (m quo: a) asFloat.
r := (m \\ a) asFloat.
BTW, does anyone know if there is any definition for the value returned
from Time millisecondClockValue except that it be a positive integer?
I think Dan added the multiply by self hash; I'm not sure why. Anyone have
any ideas?
Dave
At 15:07 -0400 6/3/99, Juan Cires Martinez wrote:
>I know this might not be common, but I have been running the same image
>on a Linux machine for the past couple of weeks (at least, I don't
>remember exactly when I started it...) and suddenly Randoms started
>generating a first random number outside of the [0,1) range,
>specifically, negative values. Further results from the same Random
>seemed OK. Since the seed initialization uses
>
> seed := Time millisecondClockValue * self hash.
>
>I checked and found out that they were being initialized to
>LargePositiveIntegers, which I'm not sure are correct values. Anybody
>know if this is the problem? I saved the image and restarted the VM and
>now my Randoms are fine. As a quick hack, the solution I adopted was to
>modify Random>>initialize once more to:
>
>initialize
> " Set a reasonable Park-Miller starting seed "
> seed := Time millisecondClockValue * (self hash + 1).
>
> a := 16r000041A7 asFloat. " magic constant = 16807 "
> m := 16r7FFFFFFF asFloat. " magic constant = 2147483647 "
> q := (m quo: a) asFloat.
> r := (m \\ a) asFloat.
> self next " forget first result (jcm)"
>
>Greetings, Juan.
_______________________________
David N. Smith
IBM T J Watson Research Center
Hawthorne, NY
_______________________________
Any opinions or recommendations
herein are those of the author
and not of his employer.
More information about the Squeak-dev
mailing list
|