Add Lehmer32 random number generator

This commit is contained in:
Alex Clink 2022-01-23 16:20:30 -05:00
parent acff191dab
commit 3ff375493d

23
src/lehmer32.cr Normal file
View file

@ -0,0 +1,23 @@
module PF
class Lehmer32
include Random
@state : UInt32
def initialize(@state = rand(type: UInt32))
end
def new_seed(n : Number)
@state = n.to_u32!
end
# Generate the next number in the squence
def next_u
@state &+= 0xe120fc15
tmp : UInt64 = @state.to_u64! &* 0x4a39b70d
m1 : UInt32 = ((tmp >> 32) ^ tmp).to_u32!
tmp = m1.to_u64! &* 0x12fad5c9
((tmp >> 32) ^ tmp).to_u32!
end
end
end