Module: RandomPerson::Ratio

Defined in:
lib/randomperson/ratio.rb

Instance Method Summary collapse

Instance Method Details

#ordmag(n) ⇒ Object

find the order of magnitude of a number for use with this module



17
18
19
20
21
22
23
24
# File 'lib/randomperson/ratio.rb', line 17

def ordmag( n )
  m = 10
  while m < 1000
    break if n.divmod(m).first == 0
    m *= 10
  end
  m
end

#presto_rangio(len, mag) ⇒ Object

use this method if you don’t have a ratio at all for an array and, hey presto! It’ll make one for you



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/randomperson/ratio.rb', line 27

def presto_rangio( len, mag )
  return [ ] unless len >= 1
  l, m = len, mag
  lower = 0
  presto = [ ]
  
  while l > 0
    # b = nil
    upper = m - l
    if l == 1
      b = upper
    else
      b = rand( upper - lower ) + lower
    end
    presto << (lower..b)   
    lower = b + 1
    l = l - 1
  end
  presto
end

#rangio(ratio = [1,1]) ⇒ Object

create a ratio that is made up of ranges to help hit with multiple valued ratios



7
8
9
10
11
12
13
14
# File 'lib/randomperson/ratio.rb', line 7

def rangio( ratio=[1,1] )
  sum = ratio.reduce(:+) #sum
  mult = 100.divmod( sum ).first #get a bigger spread of numbers
  ratio.map! { |n| n * mult }
  new_ratio = ratio.inject([0..0]) {|acc,n| acc + [acc.last.last ... (n + acc.last.last)] }
  new_ratio.shift #get rid of 0..0
  return new_ratio
end