アイスバー問題
https://gyazo.com/3ed817459fd1c707489b3f0b774de132.png
1本100円のアイスバーがある
4本に1本当たりがある
平均すると100円で何本食べられるか?
当たったアイスバーがまた当たる可能性があるから計算が必要
当たったアイスバーが当たらないならば平均1.25本になるが実際はもっと多いはず
code:ruby
trial = 0.0
count = 0.0
while true do
trial += 1
count += 1
while rand < 0.25 do
count += 1
end
puts count / trial
end
再帰を使う意義は無いと思うけど 増井俊之.icon
末尾再帰が繰り返しと同じだという例にはなってるか? code:ruby
@pay = 0 # 払った金
@count = 0 # 食べた数
def challenge
@count += 1
challenge if rand < 0.25
end
while true
@pay += 1
challenge
puts @count.to_f / @pay
end