0% found this document useful (0 votes)
14 views9 pages

Simulations of Random Variables and Class Construction

This document discusses simulations of random variables and class definitions and usage in VBA. It includes code snippets for simulations of uniform(0,1), Poisson, and uniform(1,2) distributions. It also covers defining classes in VBA by declaring private member data and public access properties, and includes an example of a class with three double properties and a method to print the class values. The document demonstrates using the class within a worksheet macro to set property values, call the print method, and transfer property values to cells.

Uploaded by

theodor_munteanu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views9 pages

Simulations of Random Variables and Class Construction

This document discusses simulations of random variables and class definitions and usage in VBA. It includes code snippets for simulations of uniform(0,1), Poisson, and uniform(1,2) distributions. It also covers defining classes in VBA by declaring private member data and public access properties, and includes an example of a class with three double properties and a method to print the class values. The document demonstrates using the class within a worksheet macro to set property values, call the print method, and transfer property values to cells.

Uploaded by

theodor_munteanu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Simulations of random

variables and class


definition and usage in VBA
Presentation made through apps
Uniform(0,1) Simulation
Sub Uniform01_simulation_click()
Dim n As Integer
n = InputBox("Enter the number of simulations you
want")
ActiveCell.FormulaR1C1 = "=RAND()"
Range(ActiveCell, ActiveCell.Offset(n, 0)).Select
Selection.FillDown
End Sub
Poisson simulation
Sub Poisson_simulation_Click()
Dim n As Integer
n = InputBox("Enter the number of simulations you
want")
ActiveCell.FormulaR1C1 = "=-LN(RAND())*0.4"
Range(ActiveCell, ActiveCell.Offset(n, 0)).Select
Selection.FillDown
End Sub
Uniform(1,2) simulation
Sub Uniform12_simulation_click()
Dim n As Integer
n = InputBox("Bla bla bla")
ActiveCell.FormulaR1C1 = "=Rand()+1"
Range(ActiveCell, ActiveCell.Offset(n, 0)).Select
Selection.FillDown
End Sub
Uniform(0,1)x(0,1) simulation
Sub Uniform0101_2D_click()
Dim n As Integer
n = InputBox("enter the number of bidimensional uniform samples:")
ActiveCell.FormulaR1C1 = "=Rand()"
ActiveCell.Offset(0, 1).FormulaR1C1 = "=Rand()"
Union(Range(ActiveCell, ActiveCell.Offset(n, 0)), _
Range(ActiveCell.Offset(0, 1), ActiveCell.Offset(n, 1))).Select
Selection.FillDown
End Sub
Classes: declaration and definition
First we declare the members data(which usually are private),and then we create public access gates.
Private x1 As Double
Private x2 As Double
Private x3 As Double
Public Property Get first() As Double
first = x1
End Property
Public Property Let first(value As Double)
x1 = value
End Property
Public Property Get second() As Double
second = x2
End Property
Public Property Let second(value As Double)
x2 = value
End Property
Class definition and declare: contd
Public Property Get third() As Double
third = x3
End Property
Public Property Let third(value As Double)
x3 = value
End Property
The difference between let and get is that get returns a
value, and let takes a value into the class data members.
Class definition and functionality
Public Sub PrintPoint()
MsgBox "(" & x1 & "," & x2 & "," & x3 & " ) "
End Sub
We will test now the class functionality inside a Worksheet in VBA.
Sub class_point()
Dim p1 As Class1
Set p1 = New Class1
p1.first = 3.1
p1.second = 3.2
p1.third = 10.1
p1.PrintPoint
End Sub
Class function
example
Sub transfer_to_cell_point()
Dim p1 As New Class1
p1.first = 2.1
p1.second = 3.3
p1.third = 4.3
p1.PrintPoint
ActiveCell.value = p1.first
ActiveCell.Offset(0, 1).value =
p1.second
ActiveCell.Offset(0, 2).value =
p1.third
End Sub

You might also like