我试图创建一个棒球赛季模拟器,看看一个糟糕的投手会对球队的总冠军赔率造成多大影响。所以我基本上为季后赛创造了一个模拟器(3轮,第一轮最好是5轮,接下来的两轮最好是7轮)。我可以看到,如果他每三场比赛投球一次,SP会对球队造成多大伤害,并且他的获胜几率降低10%。
为了确保我写这个,我首先运行我的模拟没有任何调整,所以每个游戏两个团队都有50%的机会赢得...(因此一个团队有12.5%的机会赢得世界系列赛然而,在3轮季后赛中,我的得分始终高于12.5。
我感觉问题出在Rnd功能上,所以我玩了一下(添加了一个功能,试图改变时间等),现在我的模拟结果是<12.5%。然后我试图麻烦拍摄并注意到在每一轮都没有赢得50%的时间......每次我重新运行模拟就是这种情况(就像第一轮将获得47%的成功然后第二轮51 %然后第三轮58%...然后下一轮将回归46%,52%,59%。
我做错了什么,或者这只是rnd函数处理不好的东西?
Public Function wRandomNumber(lowerbound, upperbound, Optional rndType = 1) As Double
Dim rndVariable As Double
'Const ms As Double = 0.000000011574
Randomize Timer
'Application.Wait Now + ms * 1
rndVariable = Rnd
If rndType = 1 Then
wRandomNumber = Int((upperbound - lowerbound + 1) * rndVariable + lowerbound)
ElseIf rndType = 2 Then
If (upperbound - lowerbound + 1) * rndVariable + lowerbound <= upperbound Then
wRandomNumber = (upperbound - lowerbound + 1) * rndVariable + lowerbound
Else
Do While (upperbound - lowerbound + 1) * rndVariable + lowerbound > upperbound
rndVariable = Rnd
If (upperbound - lowerbound + 1) * rndVariable + lowerbound <= upperbound Then
wRandomNumber = (upperbound - lowerbound + 1) * rndVariable + lowerbound
End If
Loop
End If
End If
End Function
Public Sub Playoff_sim()
Dim game_num As Integer
Dim lds_wins_stros As Integer
Dim lds_loss_stros As Integer
Dim lcs_wins_stros As Integer
Dim lcs_loss_stros As Integer
Dim ws_wins_stros As Integer
Dim ws_loss_stros As Integer
Dim p As Double
Dim outcome1 As Single
Dim outcome2 As Single
Dim outcome3 As Single
Dim i As Double
Dim iter As Double
Dim CHAMPS As Double
Dim Loser As Double
Dim p_bad As Double
Dim p_norm As Double
Dim lds_champs As Double
Dim lcs_champs As Double
Dim r As Double
Const ms As Double = 0.000000011574
iter = 100000
CHAMPS = 0
Loser = 0
lds_champs = 0
lcs_champs = 0
p_bad = 0.5
p_norm = 0.5
r = 0
For i = 1 To iter
game_num = 1
lds_wins_stros = 0
lds_loss_stros = 0
lcs_wins_stros = 0
lcs_loss_stros = 0
ws_loss_stros = 0
ws_wins_stros = 0
Do Until lds_wins_stros = 3 Or lds_loss_stros = 3
outcome1 = wRandomNumber(0, 1, 2)
If game_num = 3 Then
p = p_bad
Else
p = p_norm
End If
If p > outcome1 Then
lds_wins_stros = lds_wins_stros + 1
Else
lds_loss_stros = lds_loss_stros + 1
End If
r = r + 1
'Worksheets("Playoff_Sim").Cells(r, 15).Value = outcome1
game_num = game_num + 1
Loop
If lds_wins_stros = 3 Then
outcome2 = wRandomNumber(0, 1, 2)
lds_champs = 1 + lds_champs
Do Until lcs_wins_stros = 4 Or lcs_loss_stros = 4
If game_num = 3 Or 6 Or 9 Or 12 Or 15 Or 18 Or 21 Then
p = p_bad
Else
p = p_norm
End If
If p > outcome2 Then
lcs_wins_stros = lcs_wins_stros + 1
Else
lcs_loss_stros = lcs_loss_stros + 1
End If
game_num = game_num + 1
Loop
If lcs_wins_stros = 4 Then
lcs_champs = lcs_champs + 1
outcome3 = wRandomNumber(0, 1, 2)
Do Until ws_wins_stros = 4 Or ws_loss_stros = 4
If game_num = 3 Or 6 Or 9 Or 12 Or 15 Or 18 Or 21 Then
p = p_bad
Else
p = p_norm
End If
If p > outcome3 Then
ws_wins_stros = ws_wins_stros + 1
Else
ws_loss_stros = ws_loss_stros + 1
End If
'r = r + 1
game_num = game_num + 1
'Worksheets("Playoff_Sim").Cells(r, 15).Value = outcome3
Loop
If ws_wins_stros = 4 Then
CHAMPS = CHAMPS + 1
Else
Loser = Loser + 1
End If
Else
Loser = Loser + 1
End If
Else
Loser = Loser + 1
End If
Next i
Worksheets("Playoff_Sim").Cells(2, 1).Value = CHAMPS
Worksheets("Playoff_Sim").Cells(2, 2).Value = Loser
Worksheets("Playoff_Sim").Cells(2, 3).Value = lds_champs
Worksheets("Playoff_Sim").Cells(2, 4).Value = lcs_champs
End Sub
答案 0 :(得分:0)
问题不仅仅是随机数(可以通过@ A.S.H建议使用Application.RandBetween
解决),但也是模拟的逻辑。两次重复的代码片段
If game_num = 3 Or 6 Or 9 Or 12 Or 15 Or 18 Or 21 Then
p = p_bad
Else
p = p_norm
End If
相当于单行p = p_bad
。这是因为条件game_num = 3 Or 6 Or 9 Or 12 Or 15 Or 18 Or 21
始终评估为True
,因为您只是将game_num
与3
进行比较,然后强制VBA解释其余数字(6,9,15, 18,21)使用True
运算符作为Or
。相反,只需:
If game_num Mod 3 = 0 Then
p = p_bad
Else
p = p_norm
End If
也可能存在其他问题,但这应该足以让你开始。