Skip to content

Instantly share code, notes, and snippets.

@macks
Last active December 27, 2016 15:25
Show Gist options
  • Save macks/fd8b92ad05795480f48e6ba081102002 to your computer and use it in GitHub Desktop.
Save macks/fd8b92ad05795480f48e6ba081102002 to your computer and use it in GitHub Desktop.
ガチャで複数の当たりを引く確率
def combination(n, r)
factorial(n) / factorial(r) / factorial(n - r)
end
def permutation(n, r)
factorial(n) / factorial(n - r)
end
$factorial = { 0 => 1 }
def factorial(n)
$factorial[n] ||= (1..n).inject(:*)
end
if __FILE__ == $0
p = 0.01 # 当選率 p のくじで
r = 5 # r 回以上の当たりを引く確率が
s = 0.9 # s 以上になるには
# 何回の試行が必要か?
x = 0.0
(r..10000).each do |n|
x += p**r * (1 - p)**(n - r) * combination(n - 1, r - 1)
if x >= s
puts "#{n}: #{x}"
break
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment