-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenteCarlo.js
113 lines (99 loc) · 2.88 KB
/
MenteCarlo.js
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
var Terminal = new ActiveXObject('Terminal.Controller');
Terminal.SetPrinter(function (sControlSequence) {
WScript.StdOut.Write(sControlSequence);
});
Terminal.HideCursor();
Terminal.SetTextColor('White', 'Black');
Terminal.ClearScreen();
MenteCarlo(genMatrix(min(Terminal.RowLength, Terminal.ColumnLength / 2) - 2));
function max(a, b) {
return a > b ? a : b;
}
function min(a, b) {
return a < b ? a : b;
}
function getDigit(n) {
var fullwidthDigits = [
String.fromCharCode(0xff10),
String.fromCharCode(0xff11),
String.fromCharCode(0xff12),
String.fromCharCode(0xff13),
String.fromCharCode(0xff14),
String.fromCharCode(0xff15),
String.fromCharCode(0xff16),
String.fromCharCode(0xff17),
String.fromCharCode(0xff18),
String.fromCharCode(0xff19)
];
return n < 10 ? fullwidthDigits[n] : n < 100 ? n : '99';
}
function genMatrix(N) {
var matrix = [];
for (var i = 0; i < N; i++) {
matrix[i] = [];
for (var j = 0; j < N; j++) {
matrix[i][j] = 0;
}
}
return matrix;
}
function PrnInfo(matrix, step, inner, outer, pie) {
var N = matrix.length;
// Print matrix
Terminal.MoveCursorToTopLeft();
Terminal.SetTextStyle('Normal');
Terminal.SetTextStyle('Bold');
WScript.StdOut.Write('Step: ' + step / 1000 + 'k ');
Terminal.SetTextStyle('Normal');
WScript.StdOut.Write('Inner: ' + inner + ' ');
Terminal.SetTextStyle('Normal');
Terminal.SetTextStyle('Dim');
WScript.StdOut.Write('Outer: ' + outer + ' ');
Terminal.SetTextStyle('Normal');
Terminal.SetTextStyle('Bold');
WScript.StdOut.Write('Pie: ' + pie + '\n');
Terminal.SetTextStyle('Normal');
return;
}
function PrnMat(matrix, step, inner, outer, pie) {
var N = matrix.length;
PrnInfo(matrix, step, inner, outer, pie);
for (var i = 1; i < N; i++) {
for (var j = 1; j < N; j++) {
// Calculate the distance from the center
var distance = Math.pow(Math.abs(i - N / 2), 2) + Math.pow(Math.abs(j - N / 2), 2);
if (distance < N / 2 * N / 2) {
Terminal.SetTextStyle('Normal');
} else {
Terminal.SetTextStyle('Dim');
}
WScript.StdOut.Write(getDigit(Math.floor(matrix[i][j] / 1000)));
}
WScript.StdOut.Write('\n');
}
}
function MenteCarlo(matrix) {
var N = matrix.length;
var step = 0;
var inner = 0;
var outer = 0;
var mid = N / 2;
for(;;) {
step += 1;
var randX = Math.random() * N;
var randY = Math.random() * N;
matrix[Math.floor(randX)][Math.floor(randY)] += 1;
// Calculate the distance from the center
var distance = Math.pow(Math.abs(randX - mid), 2) + Math.pow(Math.abs(randY - mid), 2);
if (distance < mid * mid) {
inner += 1;
} else {
outer += 1;
}
if (step % 30000 == 0) {
PrnMat(matrix, step, inner, outer, inner * 4 / (inner + outer));
} else if (step % 1000 == 0) {
PrnInfo(matrix, step, inner, outer, inner * 4 / (inner + outer));
}
}
}