モンテカルロ法で球の体積を求めるプログラムを作らなきゃないんだけど、だれか頼む… とりあえず、円周率を求めるヤツはこうなってる。 Sub montecario() 'monte cario simukation n = 0 hit = 0 For i = 1 To 50 For j = 1 To 100 fx = Rnd(): fy = Rnd() z = fx ^ 2 + fy ^ 2 If z <= 1 Then hit = hit + 1 Else GoTo label End If label: Next j n = n + 100 p = hit / n Cells(i, 2) = p * 4 Cells(i, 1) = n Next i End Sub
Sub montecarlo() 'monte carlo simulation n = 0 hit = 0 For i = 1 To 50 For j = 1 To 100 fx = Rnd(): fy = Rnd(): fz = Rnd() z = fx ^ 2 + fy ^ 2 + fz ^ 2 If z <= 1 Then hit = hit + 1 End If Next j n = n + 100 p = hit / n Cells(i, 2) = p * 8 Cells(i, 1) = n Next i End Sub