Skip to content

Instantly share code, notes, and snippets.

@siers
Created August 28, 2012 13:13
Show Gist options
  • Save siers/3497864 to your computer and use it in GitHub Desktop.
Save siers/3497864 to your computer and use it in GitHub Desktop.
Dice sum distribution
#!/usr/bin/env ruby
T = 50000
def n
rand(6) + 1
end
def title(x)
puts x
puts "*" * x.length
end
def calculateDistribution
x = {}
x.default = 0
z = x.dup
T.times do
a = n
b = n
c = a + b
[x, z].each do |h|
h[a + b] += 1
end
x[a] += 1
x[b] += 1 unless a == b
end
[x, z]
end
def drawDistr(d, t)
title t
total = d.values.reduce :+
d.to_a.sort.each do |thrown, count|
percentage = count.to_f / total * 100
puts("%2i has been thrown %4i times accounting for %2.2f%%." % [thrown, count, percentage])
end
end
title "Backgammon: two dice sum distribution"
puts
drawDistr calculateDistribution[0], "Distribution percentage with numbers divided."
puts
drawDistr calculateDistribution[1], "Distribution percentage without numbers divided."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment