2. Bernoulli Trials


Index

Example 2a: Repeated tosses of a fair coin, outputting the history to a grid. The user inputs the probability of heads. Each time the "Toss Coin" command button is clicked, a toss of the coin is simulated with the given probability of heads and three quantities are output: the toss (trial) number, the outcome (Heads or Tails), and the relative frequency of heads (the ratio of the number of heads to the number of trials) over all trials that have been run since the start or the last restart. The results of the trial are added to a grid which keeps a history of the results. A command button that allows the user to clear all results and restart from the beginning at any time.

Example 2b: Repeated throws of a fair die. The user defines an event which consists of a subset of the numbers 1,...,6. The subset is determined by selecting the corresponding check boxes. The probability of an outcome being in this set (1/6,...,6/6) is output and the occurrence of the event on a given trial is regarded as "Success". Each time the "Throw Die" command button is clicked, a throw of a fair die is simulated and three quantities are output: the throw (trial) number, the outcome 1,...,6, and the relative freqency of success (the ratio of the number of successes to the number of trials) over all trials that have been run since the start or the last restart.

Problem 2: Repeated throws of a fair die, outputting the history to a grid. Extend Example 2a to include outputting the history to a grid similarly to Example 2b.


Example 2a. Repeated tosses of a fair coin, outputting the history to a grid.


Event Procedures:

' In (declarations) section of (General)

' Programmer:   R. Berman 
' Date:         June, 1996 
' Title:        Repeated Tosses of a Coin 
' Description:  This program simulates a coin toss experiment 
'               where the probability p of heads is input by 
'               the user. Each time the "Toss Coin" Command 
'               Button is pressed, the program outputs the toss 
'               number, the outcome (Heads or Tails), and the 
'               relative frequency of heads. After each run it 
'               adds the results to the grid. The user can restart 
'               from the beginning at any time. 
' Version       Microsoft Visual Basic 4.0 

' Require explicit declaration of variables. 
Option Explicit 

' Declare a module-level variable of single datatype to store the 
' user input of the probability of heads. The "m" in the indentifier 
' (the name of the variable) is used because of the naming convention 
' and is a reminder that the scope of the variable is module level. 
' Similarly, the first "i" indicates Integer data type. 
Dim ms_Probability As Single 

' Declare module-level variables of integer datatype to keep a running 
' total of the number of tosses and heads that have occurred since the 
' start or the last reinitialization. 
Dim mi_TotalTosses As Integer 
Dim mi_TotalHeads As Integer 

Private Sub ClearOutput() 
     
    ' Clear the output labels. 
    lblOutput(0).Caption = "" 
    lblOutput(1).Caption = "" 
    lblOutput(2).Caption = "" 
     
End Sub 

Sub InitializeGrid() 

    ' Set up the basic characteristics of the grid. 
    grdList.Rows = 2 
    grdList.Cols = 3 
    grdList.FixedCols = 1 
    grdList.FixedRows = 1 
    
    ' Align columns as centered. 
    grdList.FixedAlignment(0) = 2 
    grdList.ColAlignment(1) = 2 
    grdList.FixedAlignment(1) = 2 
    grdList.ColAlignment(2) = 2 
    grdList.FixedAlignment(2) = 2 

    ' Locate in row 0 and column 0. 
    grdList.Row = 0 
    grdList.Col = 0 
     
    ' Assign column titles. 
    grdList.Text = "Trial" 
    grdList.Col = 1 
    grdList.Text = "Outcome" 
    grdList.Col = 2 
    grdList.Text = "Rel Freq" 
     
    ' Clear values in the first row. 
    grdList.Row = 1 
    grdList.Col = 0 
    grdList.Text = "" 
    grdList.Col = 1 
    grdList.Text = "" 
    grdList.Col = 2 
    grdList.Text = "" 
    
    ' Set the column widths to just under one half inch for the 
    ' first and the remaining two to just under one inch. 
    grdList.ColWidth(0) = 600 
    grdList.ColWidth(1) = 1200 
    grdList.ColWidth(2) = 1200 
     
    ' Set grid width to the minimum needed to hold data and a vertical
    ' scroll bar which appears when the list gets longer than the height
    ' of the grid control. 
    grdList.Width = 600 + 1200 + 1200 + 400 
     
End Sub 

Private Sub cmdExit_Click() 

    ' Call the corresponding menu click procedure. 
    mnuFileExit_Click 
     
End Sub 

Private Sub cmdRestart_Click() 

    mnuFileRestart_Click 
     
End Sub 

Private Sub cmdTossCoin_Click() 

    ' Call the corresponding menu click procedure. 
    mnuFileTossCoin_Click 
     
End Sub 
     
Private Sub Form_Load() 

    ' Center the form in the screen. 
    Top = (Screen.Height - Height) / 2 
    Left = (Screen.Width - Width) / 2 

    ' Call the Sub procedure that initializes the grid. 
    InitializeGrid 

End Sub 

Private Sub mnuFileExit_Click() 

    ' End program.
    End
        
    ' Remark: If you nave a main switchboard form called "frmMain", you can return
    ' to it using the following commands.
    ' Hide 
    ' frmMain.Show 
     
End Sub 

Private Sub mnuFileRestart_Click() 

    ' Clear the output labels. 
    ClearOutput 
     
    ' Reinitialize the grid. 
    InitializeGrid 
         
    ' Reset module-level variables to 0 
    mi_TotalTosses = 0 
    mi_TotalHeads = 0 
  
End Sub 

Private Sub mnuFileTossCoin_Click() 

    ' Declare a variable to store the relative frequency 
    ' after toss. 
    Dim s_RelFreq 
     
    ' Recall that ms_Probability is a module-level 
    ' variable of single datatype to store the user 
    ' input of the probability of heads. 
     
    ' Get the user input for the probability and 
    ' proceed with the calcuations only if the input 
    ' is valid. 
     
    If b_GetInput() Then 
     
        ' Increment the variable keeping track of the number 
        ' of tosses. 
        mi_TotalTosses = mi_TotalTosses + 1 
                 
        ' Output the number of tosses. 
        lblOutput(1).Caption = Str$(mi_TotalTosses) 
         
        ' Seed the random number generator to with the 
        ' present time. 
        Randomize Timer 
     
        ' Output "Heads" or "Tails" according to whether 
        ' the random number is less than the user input 
        ' of the probability or not. Also, if the toss 
        ' results in head, increment the corresponding 
        ' variable. 
        If Rnd < ms_Probability Then 
            lblOutput(0).Caption = "Heads" 
            mi_TotalHeads = mi_TotalHeads + 1 
        Else 
            lblOutput(0).Caption = "Tails" 
        End If 
         
        ' Compute the relative frequncy and update the 
        ' correpsonding output Label and add a row to 
        ' the grid recording the results of the toss. 
        s_RelFreq = mi_TotalHeads / mi_TotalTosses 
        lblOutput(2).Caption = Format$(s_RelFreq, "Standard") 
        If mi_TotalTosses = 1 Then 
            grdList.Row = 1 
            grdList.Col = 0 
            grdList.Text = Str$(mi_TotalTosses) 
            grdList.Col = 1 
            grdList.Text = lblOutput(0).Caption 
            grdList.Col = 2 
            grdList.Text = Format$(s_RelFreq, "Standard") 
        Else 
            grdList.AddItem Str$(mi_TotalTosses) & Chr$(9) & lblOutput(0).Caption & Chr$(9) _
                & Format$(s_RelFreq, "Standard") 
        End If 

    End If 
     
End Sub 

Private Sub mnuHelpAbout_Click() 

    ' Set the caption property of the About Form 
    ' and various labels which supply information. 
    ' Note that the syntax 
    ' "formname!controlname.property" 
    ' is used to refer to a control on a different 
    ' form in the project. 
    frmAbout.Caption = "About " & Caption 
    frmAbout!lblTitle.Caption = Caption 
    frmAbout!lblProgrammer.Caption = "Written by R. Berman" 
    frmAbout!lblVersion.Caption = "Version 1" 
    frmAbout!lblDate.Caption = "May, 1996" 
     
    ' Open the About Form. 
    frmAbout.Show 
     
End Sub 

Private Sub mnuHelpConventions_Click() 

    ' The purpose of this procedure is to display in a message 
    ' box the following information. 
     
' CONVENTIONS USED IN THIS PROGRAM: 
' 
' (I) Controls 
' 
' TYPE              PREFIX 
' Text Box          txt 
' Label             lbl 
' Command Button    cmd 
' Grid              grd 
' 
' Controls that remain unchanged during the running of the program 
' are not required to conform to this naming convention. These 
' typically include Labels and Shapes (including lines and 
' rectangles) when used. 
' 
' (II) Variables 
' 
' All variables start with a prefix that indicate the scope and 
' data type. Here, the prefix consists of all letters preceding 
' the first underscore in a variable name, and the scope 
' designation precedes the data type designation. 
' 
' Scope: 
' 
' m = module-level declared in the (declarations) section of (General) 
' l = local declared within the procedure in which the variable is used; 
'     this is usually omitted. 
' 
' Data Type: 
' 
' s = Single Precision (real number) 
' i = Integer 
' st = String 
' b = Boolean (True or False) 
    ' Declare 3 variables for the arguments of the message box. 
    ' The first is the actual message, the second is a number 
    ' determining the icon exhibited in the box (we use the 
    ' built-in VB constant vbInformation which equals 48), and 
    ' the third is the title of the message box. 
    Dim st_Msg As String 
    Dim i_MsgDialog As Integer 
    Dim st_Title As String 
     
    ' Declare a String variable to contain a a line feed and 
    ' carriage return  for the purpose of formatting the message 
    ' in the message box. 
    Dim CRLF As String 
     
    ' Assign suitable values and strings to the the variables. 
    CRLF = Chr$(10) & Chr$(13) 
     
    ' Note the Space(n) function inserts n spaces in a string. 
    ' The choices of the numbers used below was made line the 
    ' columns up and was accomplished by trial and error. Since 
    ' the message box uses proportional fonts, it is not sufficient 
    ' to simply count letters. 
    st_Msg = st_Msg & "(I) Controls" & CRLF & CRLF 
    st_Msg = st_Msg & "TYPE" & Space(28) & "PREFIX" & CRLF 
    st_Msg = st_Msg & "Text Box" & Space(28) & "txt" & CRLF 
    st_Msg = st_Msg & "Label" & Space(34) & "lbl" & CRLF 
    st_Msg = st_Msg & "Command Button" & Space(11) & "cmd" & CRLF 
    st_Msg = st_Msg & "Grid" & Space(37) & "grd" & CRLF & CRLF 
    st_Msg = st_Msg & "Controls that remain unchanged throughout the program " 
    st_Msg = st_Msg & "are exempt from the naming convention. These " 
    st_Msg = st_Msg & "typically include Labels and Shapes (such as lines and " 
    st_Msg = st_Msg & "rectangles)." & CRLF & CRLF 
    st_Msg = st_Msg & "(II) Variables" & CRLF & CRLF 
    st_Msg = st_Msg & "All variables start with a prefix that indicate the scope and " 
    st_Msg = st_Msg & "data type. Here, the prefix consists of all letters preceding " 
    st_Msg = st_Msg & "the first underscore in a variable name, and the scope " 
    st_Msg = st_Msg & "designation precedes the data type designation." & CRLF & CRLF 
    st_Msg = st_Msg & "SCOPE:" & CRLF 
    st_Msg = st_Msg & "m = module-level declared in the (declarations) section of (General)" _
                & CRLF 
    st_Msg = st_Msg & "l = local declared within the procedure where variable used " 
    st_Msg = st_Msg & "(usually omitted)." & CRLF 
    st_Msg = st_Msg & "DATA TYPE:" & CRLF 
    st_Msg = st_Msg & "s = Single (single-precision floating-point), 32-bit (4-byte), "
    st_Msg = st_Msg & "approx. 6 digit accuracy." & CRLF 
    st_Msg = st_Msg & "i = Integer, " & " 16-bit (2-byte) whole numbers from "
    st_Msg = st_Msg & "-32,768 to 32,767" &  CRLF 
    st_Msg = st_Msg & "st = String" & CRLF 
    st_Msg = st_Msg & "b = Boolean (True or False)" & CRLF 
     
    i_MsgDialog = vbInformation 
    st_Title = "Conventions used in this Program" 
     
    ' Display the message box. 
    MsgBox st_Msg, i_MsgDialog, st_Title 

End Sub 

Private Sub mnuHelpInfo_Click() 

    ' Declare 3 variables for the arguments of the message box. 
    ' The first is the actual message, the second is a number 
    ' determining the icon exhibited in the box (we use the 
    ' built-in VB constant vbInformation which equals 48), and 
    ' the third is the title of the message box. 
    Dim st_Msg As String 
    Dim i_MsgDialog As Integer 
    Dim st_Title As String 
     
    ' Assign suitable values and strings to the the variables. 
    st_Msg = "This program simulates a coin toss experiment " 
    st_Msg = st_Msg & "where the probability p of heads is input by " 
    st_Msg = st_Msg & "the user. Each time the Toss Coin Command " 
    st_Msg = st_Msg & "Button is pressed, the program outputs the toss number, " 
    st_Msg = st_Msg & "the outcome (Heads or Tails), and the relative frequency " 
    st_Msg = st_Msg & "of heads. After each run it adds the results to the grid. " 
    st_Msg = st_Msg & "The user can restart from the beginning " 
    st_Msg = st_Msg & "at any time." 
    i_MsgDialog = vbInformation 
    st_Title = "Information" 
     
    ' Display the message box. 
    MsgBox st_Msg, i_MsgDialog, st_Title 

End Sub 

Private Sub txtProbability_Change() 
 
    ' If the user changes the probability of heads, 
    ' then the outcome of Heads or Tails from the 
    ' last toss of the coin is cleared. 
    ' Clear the output labels. 
    ClearOutput

End Sub 

Private Function b_GetInput() As Boolean 
 
   ' This function is set up so that it gets the user input of the 
    ' probability of heads, and returns a value of True (-1) if the 
    ' input satisfies the input criteria and returns False (0) 
    ' otherwise. 
         
    ' Declare 3 variables for the arguments of the message box. The 
    ' first is the actual message, the second is a number determining 
    ' the icon exhibited in the box (we use the built-in VB constant 
    ' vbExclamation which equals 48), the third is the title of the 
    ' message box. 
    Dim st_Msg As String 
    Dim i_MsgDialog As Integer 
    Dim st_Title As String 
     
    ' Get the user input of the probability and assign 
    ' the value to a module-level variable. 
    ms_Probability = Val(txtProbability.Text) 
     
    ' Check that the value of the probability is between 0 and 1 and 
    ' that the toss number is less than 2,000. The latter is a 
    ' restriction imposed by the grid control which allows no more than 
    ' 2,000 rows. 
    If (ms_Probability >= 0) And (ms_Probability <= 1) _ 
        And mi_TotalTosses < 2000 Then 
        b_GetInput = True 
    Else 
        st_Msg = "The probability should be between 0 and 1 and there can " 
        st_Msg = st_Msg & "be at most 2000 tosses." 
        i_MsgDialog = vbExclamation 
        st_Title = "Change Input" 
        MsgBox st_Msg, i_MsgDialog, st_Title 
        b_GetInput = False 
    End If 

End Function 

Object, Property and Settings:

Object Class    Property        Setting
Form            Name            frmCoinOne
                Appearance      0  'Flat 
                BackColor       &H00400000& 
                BorderStyle     1  'Fixed Single 
                Caption         "Multiple Coin Tosses" 
                ControlBox      0   'False 
                ForeColor       &H80000008& 
                MaxButton       0   'False 
                MinButton       0   'False 
CommandButton   Name            cmdRestart  
                Appearance      0  'Flat 
                BackColor       &H80000005& 
                Caption         "&Restart" 
                TabIndex        10 
TextBox         Name            txtProbability  
                Alignment       2  'Center 
                Appearance      0  'Flat 
                MultiLine       -1  'True 
                TabIndex        1 
CommandButton   Name            cmdExit  
                Appearance      0  'Flat 
                BackColor       &H80000005& 
                Cancel          -1  'True 
                Caption         "E&xit" 
                TabIndex        11 
CommandButton   Name            cmdTossCoin  
                Appearance      0  'Flat 
                BackColor       &H80000005& 
                Caption         "&Toss Coin" 
                Default         -1  'True 
                TabIndex        9 
Label           Name            lblOutput  
                Alignment       2  'Center 
                BackColor       &H00000080& 
                BorderStyle     1  'Fixed Single 
                ForeColor       &H00FFFFFF& 
                Index           2 
                TabIndex        7 
Label           Name            lblOutput  
                Alignment       2  'Center 
                BackColor       &H00000080& 
                BorderStyle     1  'Fixed Single 
                ForeColor       &H00FFFFFF& 
                Index           1 
                TabIndex        3 
Label           Name            Label3  
                Alignment       1  'Right Justify 
                Appearance      0  'Flat 
                BackColor       &H80000005& 
                BackStyle       0  'Transparent 
                Caption         "Toss Number:" 
                ForeColor       &H0000FFFF& 
                TabIndex        2 
Label           Name            Label4  
                Alignment       1  'Right Justify 
                Appearance      0  'Flat 
                BackColor       &H80000005& 
                BackStyle       0  'Transparent 
                Caption         "Relative Frequency:" 
                ForeColor       &H0000FFFF& 
                TabIndex        6 
Grid            Name            grdList  
                TabIndex        8 
Label           Name            lblOutput  
                Alignment       2  'Center 
                BackColor       &H00000080& 
                BorderStyle     1  'Fixed Single 
                ForeColor       &H00FFFFFF& 
                Index           0 
                TabIndex        5 
Label           Name            Label2  
                Alignment       1  'Right Justify 
                Appearance      0  'Flat 
                BackColor       &H80000005& 
                BackStyle       0  'Transparent 
                Caption         "Outcome:" 
                ForeColor       &H0000FFFF& 
                TabIndex        4 
Label           Name            Label1  
                Alignment       1  'Right Justify 
                Appearance      0  'Flat 
                BackColor       &H00FFFFFF& 
                BackStyle       0  'Transparent 
                Caption         "The Probability of Heads:" 
                ForeColor       &H0000FFFF& 
                TabIndex        0 
Menu            Name            mnuFile  
                Caption         "&File" 
Menu            Name            mnuFileTossCoin  
                Caption         "&Toss Coin" 
                Shortcut        ^T 
Menu            Name            mnuFileRestart  
                Caption         "&Restart" 
                Shortcut        ^R 
Menu            Name            mnuFileSep1            
                Caption         "-" 
Menu            Name            mnuFileExit  
                Caption         "E&xit" 
                Shortcut        ^X 
Menu            Name            mnuHelp  
                Caption         "&Help" 
Menu            Name            mnuHelpInfo  
                Caption         "&Information" 
                Shortcut        ^I 
Menu            Name            mnuHelpConventions  
                Caption         "&Conventions" 
                Shortcut        ^C 
Menu            Name            mnuHelpAbout  
                Caption         "&About" 
                Shortcut        ^A 



Example 2b. Repeated throws of a fair die.

Event Procedures:

' In (declarations) section of (General)

' Programmer:   R. Berman 
' Date:         June, 1996 
' Title:        Repeated Throws of a Fair Die 
' Description:  This program simulates repeated throws of a fair die, 
'               one by one. The user selects any subset of the check 
'               boxes signifying the faces 1 to 6. This determines 
'               the event to be regarded as success. The probability 
'               of that event (1/6,...,6/6) is updated each time the 
'               a check box is clicked. When the "Throw Die" Command 
'               Button is clicked, the program outputs the trial number, 
'               the outcome (1, 2, 3, 4, 5, 6), and the relative frequency 
'               of successes. The user can restart from the beginning at 
'               any time. 
' Version       Microsoft Visual Basic 4.0 

' Require explicit declaration of variables. 
Option Explicit 

' Declare module-level variables of Integer data type to keep running 
' totals of the number of tosses the number of heads that have occurred 
' since the start or the last reinitialization. Recall that the "m" in 
' the indentifier (the name of the variable) is used in accordance with 
' the naming convention and is a reminder that the scope of the variable 
' is module level. The first "i" indicates Integer data type. 
Dim mi_TotalTosses As Integer 
Dim mi_TotalSuccesses As Integer 

Private Sub ClearOutput() 
     
    ' Clear the output labels. 
    lblOutput(0).Caption = "" 
    lblOutput(1).Caption = "" 
    lblOutput(2).Caption = "" 
     
    ' Reset the outcome label to its default. 
    ' According to the outcome when the die is thrown, this label us updated 
    ' to "Success" or "Failure" 
    lblOutcome.Caption = "Outcome:" 
     
End Sub 

Private Sub chkNumber_Click(Index As Integer) 

    ' The check boxes used to signify the faces of the die are implemented 
    ' as a control array. Regardless of which one of the six is clicked, 
    ' this procedure is called. If it necessary to take an action based on 
    ' the one that has been clicked, then it is necessary to know which one;
    ' that information is contained in the "Index" variable. However, in 
    ' this procedure it is not important which has been clicked since the 
    ' same actions will be taken regardless. 
     
    ' Declare an Integer index variable for a loop counter. 
    Dim i_Index As Integer 
     
    ' Declare a Single variable to store the probability (1/6, ... , 6/6) 
    ' of success based on the number of faces checked. 
    Dim s_Probability As Single 
     
    ' After one of the check boxes is clicked, the output is out of date 
    ' so we clear it from the output labels. 
    ClearOutput 
     
    ' Loop through the six Check Boxes and compute the probability by summing 
    ' up 1/6 of their values. Note that the value of a Check Box is 0 if 
    ' unchecked and 1 if checked. 
    For i_Index = 1 To 6 
         
        s_Probability = s_Probability + chkNumber(i_Index).Value / 6 
     
    Next i_Index 
         
    ' Finally, update the probability in the appropriate label outputting 
    ' the result in "Standard" format (rounded to 2 decimal places). 
    lblProbability.Caption = Format$(s_Probability, "Standard") 

End Sub 

Private Sub cmdExit_Click() 

    ' Call the corresponding menu click procedure. 
    mnuFileExit_Click 
     
End Sub 

Private Sub cmdRestart_Click() 

    ' Call the corresponding menu click procedure. 
    mnuFileRestart_Click 
     
End Sub 

Private Sub cmdThrowDie_Click() 

    ' Call the corresponding menu click procedure. 
    mnuFileThrowDie_Click 

End Sub 

Private Sub Form_Load() 

    ' Center the form in the screen. 
    Top = (Screen.Height - Height) / 2 
    Left = (Screen.Width - Width) / 2 

    ' Seed the random number generator to with the present time. 
    Randomize Timer 

End Sub 

Private Sub mnuFileExit_Click() 

    ' Return to the Main Form assuming that a main switchboard 
    ' type form is being used. Alternatively, the program could be 
    ' ended with the "End" command. 
    Hide 
    frmBernMain.Show 
     
End Sub 

Private Sub mnuFileRestart_Click() 

    ' Clear the output labels. 
    ClearOutput 
     
    ' Reset module-level variables to 0. 
    mi_TotalTosses = 0 
    mi_TotalSuccesses = 0 

End Sub 

Private Sub mnuFileThrowDie_Click() 

    ' Declare a variable of Integer data type to store outcome (1,...,6) 
    ' of a throw of the die. 
    Dim i_Outcome As Integer 

    ' Declare a variable to store the relative frequency after each throw. 
    Dim s_RelFreq 
     
    ' Declare a String variable be assigned one of the the strings 
    ' "Success" or "Failure" depending on the outcome. 
    Dim st_SuccessFailure As String 
     
    ' Increment the variable keeping track of the number of tosses. 
    mi_TotalTosses = mi_TotalTosses + 1 
                 
    ' Output the number of tosses. 
    lblOutput(1).Caption = Str$(mi_TotalTosses) 
     
    ' Use the random number generator to generate the outcome of the 
    ' face of the die and output that result. 
    i_Outcome = Int(6 * Rnd + 1) 
    lblOutput(0).Caption = Str$(i_Outcome) 
             
    ' Check whether the Check Box corresponding to the outcome on the face 
    ' of the die is checked. If so the "Value" property of the control Returns 
    ' 1 which is treated as True an IF-THEN statement. Recall that 0 is treated 
    ' as false, and all other integer values as True. 
         
    ' Determine whether or not the outcome corresponds to a number that has 
    ' been checked. 
    If chkNumber(i_Outcome).Value Then 
        ' In this case the Check Box is checked and the result is regarded 
        ' as Success. Assign the string "Success" to the corresponding variable 
        ' and increment the counter for the number of successes by 1. 
        st_SuccessFailure = "Success" 
        mi_TotalSuccesses = mi_TotalSuccesses + 1 
    Else 
        ' In this case the Check Box is unchecked and the result is regarded 
        ' as Failure. 
        st_SuccessFailure = "Failure" 
    End If 
        
    ' Depending of the result, update the label next to the outcome to be "Success:" 
    ' or "Failure:". 
    lblOutcome.Caption = st_SuccessFailure & ":" 
      
    ' Compute the relative frequncy of success and update the correpsonding output 
    ' Label. 
    s_RelFreq = mi_TotalSuccesses / mi_TotalTosses 
    lblOutput(2).Caption = Format$(s_RelFreq, "Standard") 
     
End Sub 

Private Sub mnuHelpAbout_Click() 

    ' The ShowAbout Sub takes 4 arguments and displays the About form 
    ' with the appropriate information filled in. The first argument is 
    ' be the caption of the form that is calling it, the second is the 
    ' programmer's name, the third is the version number, and the fourth 
    ' is the date. 
    ShowAbout Caption, "R. Berman", 1, "June, 1996" 
     
    ' Note: The ShowAbout Sub can be included in this form. However, if there 
    ' are a number of separate forms that are calling it, it is more convenient 
    ' to put it in a separate module; this automatically makes it accessible to 
    ' all forms within the application. 
     
End Sub 

Private Sub mnuHelpConvControls_Click() 

    ' Call a procedure to show a message box with the naming conventions 
    ' for Controls. This procedure may be placed in this form. However, 
    ' if there are a number of separate forms that are calling it, it is 
    ' more convenient to put it in a separate module; this automatically 
    ' makes it accessible to all forms within the application. 
    ShowConvControls 

End Sub 

Private Sub mnuHelpConvVariables_Click() 

    ' Call a procedure to show a message box with the naming conventions 
    ' for Variables. This procedure may be placed in this form. However, 
    ' if there are a number of separate forms that are calling it, it is 
    ' more convenient to put it in a separate module; this automatically 
    ' makes it accessible to all forms within the application. 
    ShowConvVariables 
     
End Sub 

Private Sub mnuHelpInfo_Click() 

    ' Declare 3 variables for the arguments of the message box. 
    ' The first is the actual message, the second is a number 
    ' determining the icon exhibited in the box (we use the 
    ' built-in VB constant vbInformation which equals 48), and 
    ' the third is the title of the message box. 
    Dim st_Msg As String 
    Dim i_MsgDialog As Integer 
    Dim st_Title As String 
     
    ' Assign suitable values and strings to the the variables. 
    st_Msg = "This program simulates repeated throws of a fair die " 
    st_Msg = st_Msg & "one by one. The user checks any subset of the check " 
    st_Msg = st_Msg & "boxes signifying the faces 1 to 6. These determine " 
    st_Msg = st_Msg & "the event to be regarded as success. The probability " 
    st_Msg = st_Msg & "of that event (1/6,...,6/6) is updated each time " 
    st_Msg = st_Msg & "a check box is clicked. When the ""Throw Die"" Command " 
    st_Msg = st_Msg & "Button is clicked, the program outputs the trial number, " 
    st_Msg = st_Msg & "the outcome (1, 2, 3, 4, 5, 6), and the relative frequency " 
    st_Msg = st_Msg & "of successes. The user can restart from the beginning at " 
    st_Msg = st_Msg & "any time." 
    i_MsgDialog = vbInformation 
    st_Title = "Information" 
     
    ' Display the message box. 
    MsgBox st_Msg, i_MsgDialog, st_Title 

End Sub 

Object, Property and Settings:

Object Class    Property        Setting
Form            Name            frmDieToss  
                Appearance      0  'Flat 
                BackColor       &H00400000& 
                BorderStyle     1  'Fixed Single 
                Caption         "Multiple Throws of a Die" 
                ForeColor       &H80000008& 
                MaxButton       0   'False 
                MinButton       0   'False 
Frame           Name            fraEvent  
                Caption         "Die Event: Success" 
                TabIndex        9 
CheckBox        Name            chkNumber  
                Caption         "5" 
                Index           5 
                 TabIndex        15 
CheckBox        Name            chkNumber  
                Caption         "4" 
                Index           4 
                TabIndex        14 
CheckBox        Name            chkNumber  
                Caption         "3" 
                Index           3 
                TabIndex        13 
CheckBox        Name            chkNumber  
                Caption         "6" 
                Index           6 
                TabIndex        12 
CheckBox        Name            chkNumber  
                Caption         "2" 
                Index           2 
                TabIndex        11 
                Value           1  'Checked 
CheckBox        Name            chkNumber  
                Caption         "1" 
                Index           1 
                TabIndex        10 
                Value           1  'Checked 
CommandButton   Name            cmdRestart  
                Appearance      0  'Flat 
                BackColor       &H80000005& 
                Caption         "&Restart" 
                TabIndex        7 
CommandButton   Name            cmdExit  
                    Appearance      0  'Flat 
                BackColor       &H80000005& 
                Cancel          -1  'True 
                Caption         "E&xit" 
                Height          375 
                TabIndex        8 
CommandButton   Name            cmdThrowDie  
                Appearance      0  'Flat 
                BackColor       &H80000005& 
                Caption         "&Throw Die" 
                Default         -1  'True 
                TabIndex        6 
Label           Name            lblProbability  
                Alignment       2  'Center 
                BackColor       &H00400000& 
                Caption         ".33" 
                ForeColor       &H00FFFFFF& 
                TabIndex        17 
Label           Name            Label0  
                Alignment       1  'Right Justify 
                Appearance      0  'Flat 
                BackColor       &H80000005& 
                BackStyle       0  'Transparent 
                Caption         "Probability:" 
                ForeColor       &H0000FFFF& 
                TabIndex        16 
Label           Name            lblOutput  
                Alignment       2  'Center 
                BackColor       &H00000080& 
                BorderStyle     1  'Fixed Single 
                ForeColor       &H00FFFFFF& 
                Index           2 
                TabIndex        5 
Label           Name            lblOutput  
                Alignment       2  'Center 
                BackColor       &H00000080& 
                BorderStyle     1  'Fixed Single 
                ForeColor       &H00FFFFFF& 
                Index           1 
                TabIndex        1 
Label           Name            Label1  
                Alignment       1  'Right Justify 
                Appearance      0  'Flat 
                BackColor       &H80000005& 
                BackStyle       0  'Transparent 
                Caption         "Toss Number:" 
                ForeColor       &H0000FFFF& 
                TabIndex        0 
Label           Name            Label4  
                Alignment       1  'Right Justify 
                Appearance      0  'Flat 
                BackColor       &H80000005& 
                BackStyle       0  'Transparent 
                Caption         "Event Relative Frequency:" 
                ForeColor       &H0000FFFF& 
                TabIndex        4 
Label           Name            lblOutput  
                Alignment       2  'Center 
                BackColor       &H00000080& 
                BorderStyle     1  'Fixed Single 
                ForeColor       &H00FFFFFF& 
                Index           0 
                TabIndex        3 
Label           Name            lblOutcome  
                Alignment       1  'Right Justify 
                Appearance      0  'Flat 
                BackColor       &H80000005& 
                BackStyle       0  'Transparent 
                Caption         "Outcome:" 
                ForeColor       &H0000FFFF& 
                TabIndex        2 
Menu            Name            mnuFile  
                Caption         "&File" 
Menu            Name            mnuFileThrowDie  
                Caption         "&Throw Die" 
                Shortcut        ^T 
Menu            Name            mnuFileRestart  
                Caption         "&Restart" 
                Shortcut        ^R 
Menu            Name            mnuFileSep1  
                Caption         "-" 
Menu            Name            mnuFileExit  
                Caption         "E&xit" 
                Shortcut        ^X 
Menu            Name            mnuHelp  
                Caption         "&Help" 
Menu            Name            mnuHelpInfo  
                Caption         "&Information" 
                Shortcut        ^I 
Menu            Name            mnuHelpConventions  
                Caption         "C&onventions" 
Menu            Name            mnuHelpConvControls  
                Caption         "Contro&ls" 
                Shortcut        ^L 
Menu            Name            mnuHelpConvVariables  
                Caption         "&Variables" 
                Shortcut        ^V 
Menu            Name            mnuHelpAbout  
                Caption         "&About" 
                Shortcut        ^A 



Problem 2: Repeated throws of a fair die, outputting the history to a grid. Extend Example 2a to include outputting the history similarly to Example 2b.