トーク:無限歩行
提供: Hakoniwapedia
実測値について
今回の編集で「動く歩数の期待値は1.62」という文を掲載しましたが、これは以下のソースコードにより算出されたものです。間違いなどがあればご指摘下さい。
# 初期位置を指定すると歩く。戻り値は歩いた歩数
def walk(x,y)
ret=0
junban=$ms.shuffle
junban.each{|pos| # 順番に処理
if x==pos[0] && y==pos[1]
# 怪獣がいる
3.times{|hoge| #3回挑戦
i=rand(6)+1
sx,sy=[x+$ax[i], y+$ay[i]] # 移動後
if (sy%2)==0 && (y%2)==1
# 行による位置調整
sx-=1
end
if sx<0 || sy<0 || sx>=$n || sy>=$n
# 外に出る
next
end
ret+=1 # 1歩動いた
x,y=sx,sy # 移動後
break
}
end
}
return ret # 移動回数
end
#1~6の移動方向
$ax=[0, 1, 1, 1, 0,-1, 0]
$ay=[0,-1, 0, 1, 1, 0,-1]
$n=12
# n*nの箱庭で、全てのマスを通行可能と仮定した場合
#ms: [[x,y],[x,y],...]
$ms= (0..$n-1).inject([]) {|sum,i| sum+(0..$n-1).map{|j| [i,j]} }
#print ms
count=2000
#count回試算する
sum=(1..count).inject(0.0){|s,na|
s+ $ms.inject(0){|s,ea|
# 初期位置を前パターンやる
s+walk(ea[0],ea[1])
}.to_f/$ms.length
}
puts sum.to_f/count
--うひょ 2011年5月25日 (水) 06:10 (UTC)