Class: Hashrander

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

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Hashrander

Generate random numbers of arbitrary size from a hash



47
48
49
50
# File 'lib/tiledenticon.rb', line 47

def initialize hash
  @hash_i = 0
  @hash = hash
end

Instance Method Details

#get_rand(sample_size, max) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/tiledenticon.rb', line 52

def get_rand sample_size, max
  i1 = @hash_i % @hash.length

  hash_str = @hash[i1, sample_size]

  # Check if wrap over length
  rem = i1 + sample_size - @hash.length
  if rem > 0
    hash_str << @hash[0, rem]
  end

  @hash_i += sample_size

  max * hash_str.hex/(16**sample_size)
end