Class: FsrsRuby::Mash

Inherits:
Object
  • Object
show all
Defined in:
lib/fsrs_ruby/alea.rb

Overview

Mash hash function for Alea PRNG

Instance Method Summary collapse

Constructor Details

#initializeMash

Returns a new instance of Mash.



10
11
12
# File 'lib/fsrs_ruby/alea.rb', line 10

def initialize
  @n = 0xefc8249d
end

Instance Method Details

#call(data) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fsrs_ruby/alea.rb', line 14

def call(data)
  data = data.to_s
  data.each_char do |char|
    @n += char.ord
    h = 0.02519603282416938 * @n
    @n = h.to_i & 0xffffffff  # >>> 0 equivalent
    h -= @n
    h *= @n
    @n = h.to_i & 0xffffffff  # >>> 0 equivalent
    h -= @n
    @n += (h * 0x100000000).to_i  # 2^32
  end
  (@n & 0xffffffff) * 2.3283064365386963e-10  # 2^-32
end