Module: RandomPerson::Generator

Defined in:
lib/randomperson/generator.rb

Overview

Handles the generation of a new person. It’s not as fun as the way humans handle this :)

Class Method Summary collapse

Class Method Details

.days_in_month(year, month) ⇒ Integer

Convenience function

Parameters:

  • year (Integer)
  • month (Integer)

Returns:

  • (Integer)


96
97
98
# File 'lib/randomperson/generator.rb', line 96

def self.days_in_month( year, month )
  ::Date.civil(year, month, -1).day
end

.make_generator(demographic) ⇒ Object

Build a demographic generator

Examples:

Generator.make_generator demographic

Parameters:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/randomperson/generator.rb', line 13

def self.make_generator( demographic )
f = -> {
    g = Generator.pick_gender( demographic.gender_ratio )
    age = Generator.pick_age( demographic.age_lower, demographic.age_upper)
    
    person = Person.new(
      :gender =>  g, 
      :age => age,
      :dob => Generator.pick_dob( age )
    )
    
    if person.gender == 'm'   
      person.first = demographic.malefirst.execute( person ) unless demographic.malefirst.nil?
    else
      person.first = demographic.femalefirst.execute( person ) unless demographic.femalefirst.nil?
    end
      
    person.last = demographic.last.execute( person ) unless demographic.last.nil? #lastname, 
    person.prefix = demographic.prefix.execute( person ) unless demographic.prefix.nil? #title 
    person.suffix = demographic.suffix.execute( person ) unless demographic.suffix.nil?#suffix
    
    person
  }

  f
end

.pick_age(lower = 0, upper = 100) ⇒ Object

TODO:

swap if wrong way round

Parameters:

  • lower (Integer) (defaults to: 0)
  • upper (Integer) (defaults to: 100)


78
79
80
# File 'lib/randomperson/generator.rb', line 78

def self.pick_age( lower=0, upper=100 )
  age =  rand(upper - lower).to_i + lower 
end

.pick_dob(age = 16) ⇒ Object

Create a date of birth relative to today.

Parameters:

  • age (Integer) (defaults to: 16)


85
86
87
88
89
90
# File 'lib/randomperson/generator.rb', line 85

def self.pick_dob( age=16 )
  year  = Time.now.year - age
  month = rand(12) + 1;
  day   = rand( days_in_month( year, month ) ) + 1
  Time.local( year, month, day )
end

.pick_gender(ratio = [1,1]) ⇒ String

Takes a ratio and uses this to randomly select either a male or a female. To get only males or only females, pass in a 0 for either gender to make sure of getting the other gender for definite.

Examples:

To only get males:

pick_gender( [1,0] )

To only get females:

pick_gender( [0,1] )

To be likely to get 2 females to every male:

pick_gender( [1,2] )

Parameters:

  • ratio (optional, Array<Integer,Integer>) (defaults to: [1,1])

    The ratio of males to females expressed in an array, e.g. [1,1] is (a chance of) one male for every female, [1,2] is one male to two females. The default is [1,1].

Returns:

  • (String)

    Returns a ‘f’ for female or an ‘m’ for male.



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/randomperson/generator.rb', line 61

def self.pick_gender( ratio=[1,1] ) #male first
  if ratio.length == 2
    return 'f' if ratio.first == 0
    return 'm' if ratio.last == 0 
  end
  rat = ratiod( ratio )
  r = rand(rat.last.last)
  case r
    when rat[0] then 'm'
    when rat[1] then 'f'
  end
end

.ratiod(ratio = [1,1]) ⇒ Object

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



43
44
45
46
47
48
49
50
# File 'lib/randomperson/generator.rb', line 43

def self.ratiod( 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