Pseudo-random numbers/Splitmix64: Difference between revisions

Added Wren
m (→‎{{header|C}}: formatting)
(Added Wren)
Line 156:
Seed: 987654321; first 1e5 Rat values histogram
Bag(0(19929) 1(19817) 2(20085) 3(20122) 4(20047)</pre>
 
=={{header|Wren}}==
{{libheader|Wren-big}}
No 64 bit integers so we use BigInt with a mask.
 
Same answers as C reference implementation above. Can't see any essential difference between that and the pseudo-code. Raku's next-int method may need intermediate masks.
<lang ecmascript>import "/big" for BigInt
 
var Const1 = BigInt.fromBaseString("9e3779b97f4a7c15", 16)
var Const2 = BigInt.fromBaseString("bf58476d1ce4e5b9", 16)
var Const3 = BigInt.fromBaseString("94d049bb133111eb", 16)
var Mask64 = (BigInt.one << 64) - BigInt.one
 
class Splitmix64 {
construct new(state) {
_state = state
}
 
nextInt {
_state = (_state + Const1) & Mask64
var z = _state
z = ((z ^ (z >> 30)) * Const2) & Mask64
z = ((z ^ (z >> 27)) * Const3) & Mask64
return (z ^ (z >> 31)) & Mask64
}
 
nextFloat { nextInt.toNum / 2.pow(64) }
}
 
var randomGen = Splitmix64.new(BigInt.new(1234567))
for (i in 0..4) System.print(randomGen.nextInt)
 
var counts = List.filled(5, 0)
randomGen = Splitmix64.new(BigInt.new(987654321))
for (i in 1..1e5) {
var i = (randomGen.nextFloat * 5).floor
counts[i] = counts[i] + 1
}
System.print("\nThe counts for 100,000 repetitions are:")
for (i in 0..4) System.print(" %(i) : %(counts[i])")</lang>
 
{{out}}
<pre>
6457827717110365317
3203168211198807973
9817491932198370423
4593380528125082431
16408922859458223821
 
The counts for 100,000 repetitions are:
0 : 20027
1 : 19892
2 : 20073
3 : 19978
4 : 20030
</pre>
9,488

edits