Module: Cyclotone::Support::Deterministic

Defined in:
lib/cyclotone/support/deterministic.rb

Class Method Summary collapse

Class Method Details

.canonical_key(value) ⇒ Object



16
17
18
# File 'lib/cyclotone/support/deterministic.rb', line 16

def canonical_key(value)
  canonical_dump(normalize(value))
end

.float(*parts) ⇒ Object



24
25
26
# File 'lib/cyclotone/support/deterministic.rb', line 24

def float(*parts)
  random(*parts).rand
end

.int(max, *parts) ⇒ Object



28
29
30
31
32
# File 'lib/cyclotone/support/deterministic.rb', line 28

def int(max, *parts)
  return 0 if max.to_i <= 0

  random(*parts).rand(max.to_i)
end

.random(*parts) ⇒ Object



20
21
22
# File 'lib/cyclotone/support/deterministic.rb', line 20

def random(*parts)
  Random.new(seed_for(*parts))
end

.seed_for(*parts) ⇒ Object



10
11
12
13
14
# File 'lib/cyclotone/support/deterministic.rb', line 10

def seed_for(*parts)
  parts.flatten.compact.reduce(17) do |memo, part|
    ((memo * 31) ^ stable_hash(normalize(part))) & 0xFFFFFFFF
  end
end