[VBA ] 蒙地卡羅模擬法求圓周率
求版上的高手幫我健檢一下,我的程式碼錯在哪裡了?
正確的話應該要從A1~C100和 D1的表格都要有值,但是卻只在C1出現了100.6402的值
以下是我的程式碼
Sub Monte_Carlo_PI()
Dim i As Long
Dim j As Long
Dim k As Long
Dim x As Double
Dim y As Double
Dim dist As Double
Dim ma As Long
ma = 100
Randomize
For i = 1 To ma
For j = 1 To ma
For k = 1 To ma
x = Rnd()
y = Rnd()
dist = (x - 0.5) ^ 2 + (y - 0.5) ^ 2
If dist <= 0.25 Then
dist = dist * 4
Else: dist = 0
End If
Next k
Next j
Next i
Worksheets("工作表2").Cells(i, 1) = x
Worksheets("工作表2").Cells(j, 2) = y
Worksheets("工作表2").Cells(k, 3) = dist
Worksheets("工作表2").Cells(1, 4) = WorksheetFunction.Average([C1:C100])
End Sub
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 163.22.18.73
※ 文章網址: https://www.ptt.cc/bbs/Visual_Basic/M.1427197248.A.450.html
→
03/24 20:11, , 1F
03/24 20:11, 1F
→
03/24 20:23, , 2F
03/24 20:23, 2F
→
03/24 22:28, , 3F
03/24 22:28, 3F
推
03/26 15:10, , 4F
03/26 15:10, 4F
→
03/26 15:10, , 5F
03/26 15:10, 5F
推
03/26 15:13, , 6F
03/26 15:13, 6F
推
03/27 13:09, , 7F
03/27 13:09, 7F
→
03/27 16:10, , 8F
03/27 16:10, 8F
→
03/27 16:11, , 9F
03/27 16:11, 9F
→
03/27 16:13, , 10F
03/27 16:13, 10F
→
03/27 16:14, , 11F
03/27 16:14, 11F
推
03/27 19:27, , 12F
03/27 19:27, 12F
推
03/27 23:39, , 13F
03/27 23:39, 13F
→
03/27 23:39, , 14F
03/27 23:39, 14F
→
03/27 23:40, , 15F
03/27 23:40, 15F
→
03/27 23:40, , 16F
03/27 23:40, 16F
→
03/28 01:58, , 17F
03/28 01:58, 17F
→
03/28 01:59, , 18F
03/28 01:59, 18F
推
03/29 10:34, , 19F
03/29 10:34, 19F
→
03/29 10:35, , 20F
03/29 10:35, 20F
→
03/29 10:35, , 21F
03/29 10:35, 21F
→
03/29 10:36, , 22F
03/29 10:36, 22F
推
03/30 10:20, , 23F
03/30 10:20, 23F
推
03/30 16:20, , 24F
03/30 16:20, 24F
→
03/30 16:21, , 25F
03/30 16:21, 25F
→
03/31 10:10, , 26F
03/31 10:10, 26F
Visual_Basic 近期熱門文章
PTT數位生活區 即時熱門文章