Ah, you also need the sign conversion applied to the uses of seed in right shifts, but that's still a simplification. Eliot (phone) On Apr 3, 2015, at 7:15 AM, Stefan Marr <smalltalk@stefan-marr.de> wrote:
Hi:
I am porting some benchmarks to Pharo, which rely on a deterministic random number generator that uses 32 bit values.
In order to have properly comparable results across platforms, it needs to generated the same sequence of numbers.
So, I am looking for efficient ways to implement it, any ideas? I am a little at a loss, with SmallInts and Long Ints, how to get the proper 32-bit values, and how to apply the correct shifts, especially the right shift can be problematic.
Code examples below.
Thanks Stefan
In Java it looks like this:
private static int seed;
// Robert Jenkins' 32 bit integer hash function. public static int random() { seed = ((seed + 0x7ed55d16) + (seed << 12)) & 0xffffffff; seed = ((seed ^ 0xc761c23c) ^ (seed >>> 19)) & 0xffffffff; seed = ((seed + 0x165667b1) + (seed << 5)) & 0xffffffff; seed = ((seed + 0xd3a2646c) ^ (seed << 9)) & 0xffffffff; seed = ((seed + 0xfd7046c5) + (seed << 3)) & 0xffffffff; seed = ((seed ^ 0xb55a4f09) ^ (seed >>> 16)) & 0xffffffff; return seed; }
In SOM Smalltalk, it looks like this:
JenkinsRandom = ( ---- | seed |
seed: val = ( seed := val )
"Robert Jenkins' 32 bit integer hash function." random = ( seed := ((seed + 2127912214 "0x7ed55d16") + (seed as32BitUnsignedValue << 12) as32BitSignedValue) as32BitSignedValue. seed := ((seed bitXor: 3345072700 "0xc761c23c") bitXor: (seed as32BitUnsignedValue >>> 19)) as32BitSignedValue. seed := ((seed + 374761393 "0x165667B1") + (seed as32BitUnsignedValue << 5) as32BitSignedValue) as32BitSignedValue. seed := ((seed + 3550635116 "0xd3a2646c") bitXor: (seed as32BitUnsignedValue << 9) as32BitSignedValue) as32BitSignedValue. seed := ((seed + 4251993797 "0xfd7046c5") + (seed as32BitUnsignedValue << 3) as32BitSignedValue) as32BitSignedValue. seed := ((seed bitXor: 3042594569 "0xb55a4f09") bitXor: (seed as32BitUnsignedValue >>> 16)) as32BitSignedValue. ^ seed ) )
-- Stefan Marr INRIA Lille - Nord Europe http://stefan-marr.de/research/