コメントにも書いたとおり、算数的には、さっぱり理解できません。
なので、プログラムで無理矢理。
class Toys COMPLETED = 0b11111111 def initialize @collection = 0 end def store (n) @collection |= (1 << n) end def to_s s = sprintf("has %08b", @collection) end def complete? @collection == COMPLETED end end class Problem def initialize @n = 0 @sum = 0 end def challenge(try_n) try_n.times { |i| n = 0 mytoys = Toys.new until mytoys.complete? toy = rand(8) mytoys.store(toy) n += 1 end @sum += n } @n += try_n end def report puts "average is #{@sum / @n.to_f}" end end p = Problem.new 10.times { p.challenge(10000) p.report }
10万回試行して、8個そろうまでにかかった回数の平均値を求めます。1万回ごとに、経過報告も。
結果は、こんな感じになりました。
average is 21.8048
average is 21.7363
average is 21.69493333
average is 21.6928
average is 21.67944
average is 21.68865
average is 21.71261429
average is 21.69645
average is 21.69582222
average is 21.69976
22回かかるっぽいです。
# もう、みんな知ってるって
算数的にも、多少納得したので、追記。
http://www2.odn.ne.jp/~cdh88520/toys.html
ここで、「これは、幾何分布の期待値です」というキーワードを入手。
確率統計論とか、そういう方面を調べてみれば良いのか。
定義と計算式が手に入れば、とりあえず納得。
幾何分布は『初めて成功するまでに失敗した回数』である離散型確率変数が従う分布である.