Skip to content

Instantly share code, notes, and snippets.

@danielmiessler
Last active September 27, 2015 10:47
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 danielmiessler/1257226 to your computer and use it in GitHub Desktop.
Save danielmiessler/1257226 to your computer and use it in GitHub Desktop.
A simple dicerolling program.
class Array
def sum
self.inject{|sum,x| sum + x }
end
end
nod = 10
noc = 1000000
target = 20
rolls = []
dicetype = 6
success = 0
failure = 0
noc.times do
nod.times do
rolls.push(1 + rand(dicetype))
end
if rolls.sum >= target :
success += 1
else
failure += 1
end
rolls.clear
end
percentage = success.to_f / noc.to_f * 100
print "When rolling one million times on #{nod} dice with a target of #{target}, there were #{success} successes and #{failure} failures, which is #{percentage}%."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment