-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Supporting algorithm 15 (VB).bas
91 lines (74 loc) · 2.69 KB
/
Supporting algorithm 15 (VB).bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
Attribute VB_Name = "Module1"
'##############################################################################################
'# John Wiley & Sons, Inc. #
'# #
'# Book: Markov Chains: From Theory To Implementation And Experimentation #
'# Author: Dr. Paul Gagniuc #
'# Data: 01/09/2016 #
'# #
'# Description: #
'# Supporting algorithm 15. A Markov Chain framework for simulation. #
'# The probability values present inside a 4x4 transition matrix (P) #
'# are directly used for an automatic generation of the letter #
'# combination that make up the representation of four jars. Thus, #
'# the four letter sequences have a calculated proportion of “A”, #
'# “B”, “C” and “D” letters. The chance of a letter chosen at random #
'# from one of the four sequences is directly dictated by the #
'# proportions of “A”, “B”, “C” and “D” letters. #
'##############################################################################################
Dim P(0 To 4, 0 To 3) As Variant
Dim Jar(1 To 4) As Variant
Private Sub main()
P(0, 0) = "A"
P(0, 1) = "B"
P(0, 2) = "C"
P(0, 3) = "D"
P(1, 0) = 0
P(1, 1) = 1
P(1, 2) = 0
P(1, 3) = 0
P(2, 0) = 0.33
P(2, 1) = 0
P(2, 2) = 0.33
P(2, 3) = 0.33
P(3, 0) = 0
P(3, 1) = 1
P(3, 2) = 0
P(3, 3) = 0
P(4, 0) = 0
P(4, 1) = 0
P(4, 2) = 1
P(4, 3) = 0
For j = 1 To 4
Jar(j) = Fill_Jar(j)
Next j
draws = 100
a = Draw(1)
For i = 1 To draws
For j = 0 To 3
If a = P(0, j) Then
a = Draw(j + 1)
z = z & P(0, j)
GoTo 1
End If
Next j
1:
Next i
MsgBox z
End Sub
Function Fill_Jar(ByVal S As Variant) As Variant
Ltot = 100
For i = 0 To 3
a = Int(Ltot * P(S, i))
For j = 1 To a
b = b & P(0, i)
Next j
Next i
Fill_Jar = b
End Function
Function Draw(ByVal S As Variant) As Variant
Randomize
randomly_choose = Int(Rnd * Len(Jar(S)))
ball = Mid(Jar(S), randomly_choose + 1, 1)
Draw = ball
End Function