Hang on. It looks like their random number generator is based on a
Jenkins' 32 bit hash function* such that the operation is:
seed := some initial value.
and then
Random>>next
seed := seed jenkinsHash.
^seed
But who said Jenkins' hash function is a good RNG when used that way?
I'd be interested to know if there was a proper citation, when I wrote
the hash book I didn't see any such thing. Did I miss the reference?
Am I reading the code right?
Lagged Fibonacci RNGs are much more Smalltalk friendly, I'd use those.
Andres.
*: it would have been nicer for the code to say *which* Jenkins hash
function, I suspect lookup3 but I didn't verify so...
On 4/3/15 7:15 , Stefan Marr 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
)
)