Module: OrefinderEstimate::Rng

Defined in:
lib/orefinder_estimate/rng.rb

Constant Summary collapse

MASK =
0xFFFFFFFF

Class Method Summary collapse

Class Method Details

.imul(a, b) ⇒ Object



19
20
21
# File 'lib/orefinder_estimate/rng.rb', line 19

def imul(a, b)
  ((a & MASK) * (b & MASK)) & MASK
end

.make_rng(parts) ⇒ Object

FNV-1a hash of parts.join('|') -> mulberry32 generator (ASCII inputs).



36
37
38
39
40
41
42
43
44
# File 'lib/orefinder_estimate/rng.rb', line 36

def make_rng(parts)
  h = 2_166_136_261
  s = parts.join('|')
  s.each_char do |ch|
    h ^= ch.ord
    h = imul(h, 16_777_619)
  end
  mulberry32(h & MASK)
end

.mulberry32(seed) ⇒ Object

Returns a lambda producing doubles in [0, 1).



24
25
26
27
28
29
30
31
32
33
# File 'lib/orefinder_estimate/rng.rb', line 24

def mulberry32(seed)
  state = seed & MASK
  lambda do
    state = (state + 0x6D2B79F5) & MASK
    t = state
    t = imul(t ^ (t >> 15), t | 1)
    t = (t ^ ((t + imul(t ^ (t >> 7), t | 61)) & MASK)) & MASK
    (t ^ (t >> 14)) / 4_294_967_296.0
  end
end

.round_half_up(x) ⇒ Object

floor(x + 0.5); matches JS Math.round (not banker's rounding).



11
12
13
# File 'lib/orefinder_estimate/rng.rb', line 11

def round_half_up(x)
  (x + 0.5).floor
end

.round_to_tenth(x) ⇒ Object



15
16
17
# File 'lib/orefinder_estimate/rng.rb', line 15

def round_to_tenth(x)
  round_half_up(x * 10) / 10.0
end