The programming language for this problem is Visual Basic What is the probabilit
ID: 3688161 • Letter: T
Question
The programming language for this problem is Visual Basic
What is the probability that the underdog will win the World Series of Baseball? What is the average number of games for a World Series? Write an animated program to answer these questions. For instance, suppose that the underdog is expected to win 40% of the time. We say that the probability of the underdog’s winning a game is 40%. (In order for a team to be the underdog, the probability that the team wins a game must be less than 50%.) Figure 9.71 shows that the probability of a 40% underdog’s winning the World Series is about 29 %, and that such a series would last an average of about 5.70 games. The program should simulate the playing of 10,000 World Series where the underdog has the probability of winning that was entered into the first text box. The values of the horizontal scroll bars should extend from 0 to 10,000 and should be calculated after each series so that the scroll boxes steadily move across the bars. Note: In order to spare Visual Basic from being overwhelmed with changing the values in the twelve text boxes to the right of the scroll bars too often, just change the values after every ten series. Also, every time the values are changed, execute a Refresh method for each of these text boxes.
Explanation / Answer
Const GAMES as Integer=10000
Private Sub Form.Load(sender As Object,e As EventArgs) Handles MyBase.Load
txtUnderdogValue.Text=0
txtFavoriteValue.Text=0
End Sub
Private Sub btnSimulate_Click(sender As Object,e As EventArgs)Handles btnSimulate.Click
If ISNumeric(txtProbability.Text) Then
for i as Integer=0 to Games
Play Game()
If i Mod 10=0 Then
updateText()
RefreshText()
End If
Next
‘after the loop,display average duration of the world series
‘multiply each game period by associated duration
‘i.e. 4 Games*4;5Games*5 etc; add them together to get the total number of game played
‘and divide by total of world series played
txtAverage.Text=FormatNumber((hsbGames4.Value*4+hsbGames5.Value*5+hsbGames6.Value*6+hsbGames7.Value*7)/GAMES,2)+”games”
Else
MsgBox(“Please enter valid probability”,MsgBoxStyle.Critical,”Invalid Entry”)
EndIf
End Sub
Private Sub UpdateText()
‘update values
txtUnderdogValue.Text=hsbUnderdog.Value
txtFavoriteValue.Text=hsbFavorite.Value
txtGames4Value.Text=hsGames4.Value
txtGames5Value.Text=hsGames5.Value
txtGames6Value.Text=hsGames6.Value
txtGames7Value.Text=hsGames7.Value
‘update percentages
txtUnderdogPercent.Text=FormatPercent(hsbUnderdog.Value/GAMES,2)
txtFavoritePercent.Text=FormatPercent(hsbFavorite.Value/GAMES,2)
txtGames4Percent.Text=FormatPercent(hsbGames4.Value/GAMES,2)
txtGames5Percent.Text=FormatPercent(hsbGames5.Value/GAMES,2)
txtGames6Percent.Text=FormatPercent(hsbGames6.Value/GAMES,2)
txtGames7Percent.Text=FormatPercent(hsbGames7.Value/GAMES,2)
Private Sub Play Game()
Dim probability as Integer=CInt(text Probability.Text)
Dim rand As New Random()
Dim gameOver as Boolean=False
Dim underdogWins As Integer=0
Dim favoriteWinAs As Integer=0
Dim totalGames As Integer=0
While gameOver=False
‘generate random number from 0% to 100%
If(rand.Next(0,100))<probability) Then
underdogWins+=1 ‘underdogWins
Else
favoriteWins+=1
End If
‘check if anyone won 4 games
If underdogWins=4 Then
gameOver=True
hsbUnderdog+=1
Elseif favoriteWins=4 Then
gameOver=True
hsbFavorite.Value+=1
End If
totalGames +=1 ‘add the game to total games played
End While
‘update game slides based on how many games were needed for any team to win
Select Case totalGames
Case 4
hsbGames4.Value+=1
Case 5
hsbGames5.Value+=1
Case 6
hsbGames6.Value+=1
Case 7
hsbGames7.Value+=1
End Select
End Sub
Private Sub RefreshText()
txtUnderdogValue.Refresh()
txtfavoriteValue.Refresh()
txtGames4Value.Refresh()
txtGames5Value.Refresh()
txtGames6Value.Refresh()
txtGames7Value.Refresh()
End Sub
End Class
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.