More on Random

David N. Smith (IBM) dnsmith at watson.ibm.com
Sat Jun 12 17:59:44 UTC 1999


At 17:28 -0400 6/11/99, Tom Burns wrote:
>[...]
>> Anyhow, how about this:
>>
>> 	seed := (Time millisecondClockValue bitAnd: 16r3FFFFFFF)
>> 			bitXor: self hash + 1.
>>
>> The #bitAnd: assures that the clock value is less than the
>> value 'm' and removes dependencies on the maximum clock value; it might
>> change some day!  The #bitXor: assures that the result is not zero, and
>the
>> addition of 1 assures against self hash answering zero. Most of this is
>> paranoia, but then that's a disease old programmers get!
>>
>> Anyone see anything I missed?
>
>Well, if the max clock value might change, so might the max SmallInteger
>value, so perhaps one should use "SmallInteger maxVal" rather than
>16r3FFFFFFF.  :-)

I wrote that reply over several hours, while doing other things, while
reading the P-M paper again, and while playing with the code, and, well,
blush, I left a bogus early version of the expression in the reply. (In
"Re: More on Random; a smaller faster P-M for Squeak", sent a few minutes
later, I somehow managed to get it right, but that's no excuse. :)

Here is the code I actually ended up with, copied just now from the image:

	seed := ((Time millisecondClockValue bitXor: self hash)
			bitAnd: 16r7FFFFFFC) + 1.

It does the #bitXor: first to get a unique value, even if two instances of
Random are obtained within one clock tick, then masks off a 'safe' portion,
leaving room at the low-order end into which to add 1 and not get carry if
the prior result was all one bits, then adds 1 to make sure the result is
not zero.

The limit on the value is not that of a small int, but that of the constant
"m" in the code which is 16r7FFFFFFF. The seed must never be equal to or
greater than "m", or be zero or less. Using the limits of a small int,
which I did in an prior version of the code, cuts the range of possible
seeds in half. It's probably no problem, but isn't necessary so I changed
it.

Sorry, and thanks to all who pointed out the flaws. I'll try to be more
careful.

Dave
_______________________________
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