mirror of
https://github.com/SleepingInsomniac/pixelfaucet
synced 2025-01-31 19:57:53 +01:00
Add Lehmer32 random number generator
This commit is contained in:
parent
acff191dab
commit
3ff375493d
1 changed files with 23 additions and 0 deletions
23
src/lehmer32.cr
Normal file
23
src/lehmer32.cr
Normal 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
|
Loading…
Add table
Reference in a new issue