Skip to content

Instantly share code, notes, and snippets.

@AJFaraday
Last active April 8, 2017 22:26
Show Gist options
  • Save AJFaraday/4aee86b0c06fbed18358225c83d85b9c to your computer and use it in GitHub Desktop.
Save AJFaraday/4aee86b0c06fbed18358225c83d85b9c to your computer and use it in GitHub Desktop.
1 million dice
x = Hash.new(0)
1000000.times.collect{ ChanceGenerator.roll_bag([6,6]).sum }.collect{|v| x[v] += 1 }
x.sort.each{|k,v| puts "#{k}: #{v} times"}
=begin
2: 27587 times
3: 55513 times
4: 83018 times
5: 111126 times
6: 138510 times
7: 167622 times
8: 138176 times
9: 111126 times
10: 83781 times
11: 55625 times
12: 27916 times
=end
require 'benchmark'
x = Benchmark.measure do
1000.times.collect{ChanceGenerator.roll_bag([6,6]).sum}
end
puts x
# 1,00 times
# 0.010000 0.000000 0.010000 ( 0.006353)
# 10,000 times
# 0.060000 0.000000 0.060000 ( 0.055263)
# 100,000 times
# 0.180000 0.000000 0.180000 ( 0.177294)
# 1,000,000 times
# 0.710000 0.000000 1.710000 ( 1.711517)
# In conclusion, the above probability spread took under 2 seconds to generate.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment