Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I would love some help especially with Parts C & D, if possible. I am having som

ID: 3810390 • Letter: I

Question

I would love some help especially with Parts C & D, if possible. I am having some difficulty with both.

PART B

Write the following three functions in a module called Simulate:

Public Function GetRandomUniform(ByVal min As Integer, ByVal max as Integer) as Integer

‘ This function returns a random number from a uniform distribution between min and max.

Public Function GetRandomNormal(ByVal mean As Single, ByVal stddev as Single) as Single

‘ This function returns a random number from a normal distribution with a mean of mean and standard ‘ deviation of stddev

Public Function GetBinIndex(ByVal mini As Single, ByVal maxi As Single, ByVal numbins as _ Integer, ByVal valuetobin as Single) As Integer

‘ This function returns the Bin Index given an output minimum of mini, output maximum of maxi, ‘ numbins number of bins, and a value to bin of valuetobin

PART C

Include the module created in part B to develop a Visual Basic .NET program that will simulate the basic profit calculation, PT = nPv, where n follows a uniform distribution, Pv follows a normal distribution, and the user can input the number of bins and number of iterations. The user must also input the min and max for n and the mean and standard deviation for Pv. Finally, the user can click a button and the results will be graphed on a bar chart using the Microsoft Chart Control and the average total profit (PT) will be displayed in a textbox. Turn in a screen shot of the resulting chart using:

1. Iterations: 10000 Bins: 5 n-min: 1 n-max: 10 Pv-mean: 9000 Pv-stddev: 1200 2. Iterations: 10000 Bins: 10 n-min: 1 n-max: 10 Pv-mean: 9000 Pv-stddev: 1200 3. Iterations: 10000 Bins: 10 n-min: 1 n-max: 10 Pv-mean: 7000 Pv-stddev: 1500

That’s three screen shots.

PART D

Extend the Visual Basic .NET program developed in part C to simulate the basic profit calculation, PT = nPv, where the user can select either a uniform or normal distribution for n using radio buttons and then must input the appropriate parameters (min and max if they select uniform, mean and standard deviation if they select normal) and they can similarly select either a uniform or normal distribution for Pv with appropriate parameters depending on the selection. Of course, the user will input the number of bins and number of iterations. Finally, the user can click a button and the results will be graphed on a bar chart using the Microsoft Chart Control and the average total profit (PT) will be displayed in a textbox. Also include in the program any necessary input validation for all input values. Turn in a listing of the code and a screen shot of the resulting chart using:

1. Iterations: 10000 Bins: 5 n-min: 1 n-max: 10 Pv-mean: 10000 Pv-stddev: 2500 2. Iterations: 10000 Bins: 10 n-mean: 6 n-stddev: 1 Pv-min: 1500 Pv-max: 9000 3. Iterations: 10000 Bins: 10 n- mean: 10 n- stddev: 3 Pv-min: 1500 Pv-max: 10000

That’s three screen shots and a listing.

Explanation / Answer

Solution:

Note: Functions are given

' On clicking the button calculate
Private Sub btnCalculat_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs)
Handles btnCalculate.Click
Dim bins, dn, dx, di As Integer
Dim dPt, dPv As Single
Dim Pt_Min, Pt_Max, Pv_Min, Pv_Max As Single
Dim n_Max, n_Min, nbins, iter As Integer
Dim Bins() As Integer   
nbins = Val(txtBins.Text)
iter = Val(TextBox1.Text)
totalProfit.Text = 0   
If radnUniform.Checked = True Then
n_Min = Val(txtNMin.Text)
n_Max = Val(txtNMax.Text)
ElseIf radnNormal.Checked = True Then
n_Min = Val(txtNMax.Text) - 3 * Val(txtNMin.Text)
n_Max = Val(txtNMax.Text) + 3 * Val(txtNMin.Text)
End If
If radPvUniform.Checked = True Then
Pv_Min = Val(txtPvMin.Text)
Pv_Max = Val(txtPvMax.Text)
Else radPvNormal.Checked = True Then
Pv_Min = Val(txtPvMax.Text) - 3 * Val(txtPvMin.Text)
Pv_Max = Val(txtPvMax.Text) + 3 * Val(txtPvMin.Text)
End If
Pt_Min = n_Min * Pv_Min
Pt_Max = n_Max * Pv_Max
ReDim Bins(nbins + 1)
For dx = 1 To iter
If radnUniform.Checked = True Then
dn = GetRandomUniform(n_Min, n_Max)
Else
dn = GetRandomNormal(Val(txtNMax.Text), Val(txtNMin.Text))
End If
If dn > n_Max Then
dn = n_Max
End If
If radPvUniform.Checked = True Then
dPv = GetRandomUniform(Pv_Min, Pv_Max)
Else
dPv = GetRandomNormal(Val(txtPvMax.Text), Val(txtPvMin.Text))
End If
If dPv > Pv_Max Then
dPv = Pv_Max
End If
dPt = dn * dPv
totalProfit.Text += dPt
di = GetBinIndex(Pt_Min, Pt_Max, nbins, dPt)
If di > nbins Then
MessageBox.Show("2Index is out of Bounds")
Exit For
End If
Bins(di) += 1
Next
       ' Set the row count as number of bins
MSChart1.RowCount = nbins
       ' Set the coloumn count as number of bins
MSChart1.ColumnCount = 1
For di = 1 To nbins
           ' Set the row count as di
MSChart1.Row = di
           ' Set the data
MSChart1.Data = Bins(di)
           ' Set the label
MSChart1.RowLabel = Pt_Min + (((Pt_Max - Pt_Min) / nbins) * (di))
Next
totalProfit.Text /= iter
totalProfit.Text = FormatNumber(totalProfit.Text, 2)
End Sub

  

Private Sub radPvUniform_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles radPvUniform.CheckedChanged
If radPvUniform.Checked = True Then
labelMax1.Visible = True
labelMin1.Visible = True
labelMean1.Visible = False
labelStaDev1.Visible = False
End If
End Sub

Private Sub radPvNormal_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles radPvNormal.CheckedChanged
If radPvNormal.Checked = True Then
labelMax1.Visible = False
labelMin1.Visible = False
labelMean1.Visible = True
labelStaDev1.Visible = True
End If
End Sub

Private Sub btnExit_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnExit.Click
End
End Sub

Private Sub radnUniform_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles radnUniform.CheckedChanged
If radnUniform.Checked = True Then
labelMax2.Visible = True
labelMin2.Visible = True
labelMean2.Visible = False
labelStaDev2.Visible = False
End If
End Sub

Private Sub radnNormal_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles radnNormal.CheckedChanged
If radnNormal.Checked = True Then
labelMax2.Visible = False
labelMin2.Visible = False
labelMean2.Visible = True
labelStaDev2.Visible = True
End If
End Sub

Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
MSChart1.RowCount = 0
End Sub


Module Simulate
' Function to get the random uniform
Public Function GetRandomUniform(ByVal min As Integer,
ByVal max As Integer) As Integer
Dim du As Integer
Randomize()
du = Int(min + Rnd() * (max - min + 1))
Return du
End Function

' Function to get the random normal
Public Function GetRandomNormal(ByVal Mean As Single,
ByVal StdDev As Single) As Single
Dim dr, dphi, dx, dz As Single
Randomize()
dr = Rnd()
dphi = Rnd()
dz = Math.Cos(2 * 3.14159 * dr) * Math.Sqrt(-2 * Math.Log(dphi))
dx = (dz * StdDev) + Mean
Return dx
End Function

' Function to get bin index
Public Function GetBinIndex(ByVal mini As Single, ByVal maxi As Single,
ByVal numbins As Integer, ByVal ValueToBin As Single) As Integer
Dim dq As Double
Dim dqc As Double
dq = CDbl(ValueToBin - mini) * (numbins / (maxi - mini))
dqc = Math.Ceiling(dq)
If dqc > numbins Then
MessageBox.Show("Out of bounds index" & dqc)
End If
Return dqc
End Function
End Module