Pseudo-random numbers/Xorshift star: Difference between revisions

m (→‎{{header|Factor}}: make it return a float instead of a ratio)
Line 185:
4 : 20007
</pre>
 
 
=={{header|Julia}}==
{{trans|Python}}const mask64 = (0x1 << 64) - 1
const mask32 = (0x1 << 32) - 1
const CONST = 0x2545F4914F6CDD1D
mutable struct XorShiftStar
state::UInt
end
XorShiftStar(_seed=0x0) = XorShiftStar(UInt(_seed) & mask64)
 
seed(x::XorShiftStar, num) = begin x.state = num & mask64 end
 
"""return random int between 0 and 2**32"""
function next_int(x::XorShiftStar)
x.state = (x.state ⊻ (x.state >> 12)) & mask64
x.state = (x.state ⊻ (x.state << 25)) & mask64
x.state = (x.state ⊻ (x.state >> 27)) & mask64
return (((x.state * CONST) & mask64) >> 32) & mask32
end
 
"""return random float between 0 and 1"""
next_float(x::XorShiftStar) = next_int(x) / (1 << 32)
function testXorShiftStar()
random_gen = XorShiftStar()
seed(random_gen, 1234567)
for i in 1:5
println(next_int(random_gen))
end
seed(random_gen, 987654321)
hist = fill(0, 5)
for _ in 1:100_000
hist[Int(floor(next_float(random_gen) * 5)) + 1] += 1
end
foreach(n -> print(n - 1, ": ", hist[n], " "), 1:5)
end
 
testXorShiftStar()
</lang>{{out}}
<pre>
3540625527
2750739987
4037983143
1993361440
3809424708
0: 20103 1: 19922 2: 19937 3: 20031 4: 20007
</pre>
 
 
=={{header|Python}}==
4,105

edits