Skip to content

Instantly share code, notes, and snippets.

@vidarh
Created March 19, 2015 10:09
Show Gist options
  • Save vidarh/3360589699389db75ea3 to your computer and use it in GitHub Desktop.
Save vidarh/3360589699389db75ea3 to your computer and use it in GitHub Desktop.
Trivially primitive simulation of survival (or not) of lineages
class Male
@@lineage = 1
def initialize
@lineage = @@lineage
@@lineage += 1
end
def inspect; "<#{@lineage}>"; end
attr_reader :lineage
end
popsize = 1000
generations = 100
# Percentage of the population we consider rich
rich_pct = 0.1
# Integer factor of how many more children the rich part of the popopulation gets
factor = 2
# Percent of "children" that die before their generation reproduce
death_pct = 0.09
tribe = Array.new(popsize) { Male.new }
(1..generations).each do |gen|
pivot = (tribe.size * rich_pct).to_i
rich = tribe[0.. pivot-1]
poor = tribe[pivot..-1]
# The new generation consists of <factor> times children of
# each rich male, and 1 child of each poor male.
tribe = rich * factor + poor
# We're not taking into account transfer of wealth at all.
# In this primitive simulation, every member of the new generation
# gets an equal chance of being rich.
tribe = tribe.shuffle
puts "Generation #{gen}"
puts " #{tribe.size} people after reproduction"
culling = (tribe.size * death_pct).to_i
puts " Culling #{culling} people"
tribe.slice!(tribe.size-culling,culling)
puts " #{tribe.size} people after deaths"
puts " There are #{tribe.collect(&:lineage).uniq.count} lineages left"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment