Skip to content

Instantly share code, notes, and snippets.

@oisin
Created Feb 27, 2016
Embed
What would you like to do?
Compute odds of receiving N instances of symbol Y in a throw of M dice with SYMBOLS faces
# Compute odds of occurrence of N symbol Y in M dice
#
symbols = ARGV[0].split(",") # ["s", "ss", "sss", "h", "b", "f"]
m = ARGV[1].to_i
n = ARGV[2].to_i
y = ARGV[3]
def to_radix(int, radix)
int == 0 ? [] : (to_radix(int / radix, radix) + [int % radix])
end
z = symbols.size
# What are the chances of throwing 6 's' with three dice
counter = 0.0
total = comb = z ** m
puts "total combinations available is #{comb}"
while (comb >= 0) do
t = to_radix(comb, z)
if t.size < m
t.insert(0, *Array.new(m - t.size, 0))
end
syms = t.collect { |sym_inx|
symbols[sym_inx]
}.join
puts "#{comb}: #{syms}"
# Increment counter if this 'roll' satisfies check
if syms.count(y) >= n
counter += 1.0
end
comb -= 1
end
puts "total throws #{total}"
puts "odds of there being #{n} occurrences of symbol #{y} in a throw of #{m} #{z}-sided dice = #{'%.2f' % (counter/total * 100.0)}%"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment