Created
October 10, 2013 03:51
-
-
Save mark/6912789 to your computer and use it in GitHub Desktop.
Stone Age—plot the distribution for # of workers & resource difficulty
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TRIALS = 100_000 | |
def d6 | |
rand(6) + 1 | |
end | |
def nd6(n) | |
sum = 0 | |
n.times { sum += d6 } | |
sum | |
end | |
def stone_age(workers, difficulty) | |
nd6(workers) / difficulty | |
end | |
def hits(workers, difficulty) | |
count = 0 | |
workers.times { count += 1 if d6 >= difficulty } | |
count | |
end | |
def hash_of_numbers | |
Hash.new { |h, k| h[k] = 0 } | |
end | |
def percentage(n) | |
per = n * 100 / TRIALS | |
per = 1 if per == 0 | |
"*" * per | |
end | |
def average(trials) | |
sum = 0 | |
trials.each { |k, v| sum += k * v } | |
sum.to_f / TRIALS | |
end | |
puts "TRIALS = #{ TRIALS }\n" | |
(2..6).each do |difficulty| | |
puts "DIFFICULTY = #{ difficulty }" | |
puts "==============" | |
puts | |
(1..10).each do |workers| | |
puts "\tWORKERS = #{ workers }" | |
puts "\t============" | |
trials = hash_of_numbers | |
TRIALS.times do |i| | |
resources = stone_age(workers, difficulty) | |
# resources = hits(workers, difficulty) | |
trials[resources] += 1 | |
end | |
trials.keys.sort.each do |key| | |
puts "\t\t#{ key }\t#{ percentage(trials[key]) }" | |
end | |
puts "\t\tAVERAGE = #{ average(trials) }" | |
puts | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment