Skip to content

Instantly share code, notes, and snippets.

@oisin
Created February 27, 2016 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oisin/ed788559e21e937789b2 to your computer and use it in GitHub Desktop.
Save oisin/ed788559e21e937789b2 to your computer and use it in GitHub Desktop.
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