def d(n)
  rand(n) + 1
end
class Fixnum
  def **(n)
    (1..self).inject(0) {|m, _| m + d(n) }
  end
end
class String
  def dice_eval
    eval self.gsub(/%/, '(100)').
              sub(/(^|[^\d)])d/, '\11d').
              gsub(/d/, '**')
  end
end
if $0 == __FILE__
  if ARGV.empty?
    puts "usage: #$0 dice [throws]\n\ne.g.: #$0 2d6 3\n7 12 2"
  else
    (ARGV[1] || 1).to_i.times do
      print ARGV[0].dice_eval, " "
    end
  end
end
