-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestCoins.html
223 lines (166 loc) · 4.62 KB
/
testCoins.html
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<!DOCTYPE html>
<html>
<head>
<script>
document.getElementById("demo1").innerHTML = "Paragraph changed.";
// Create a Map
const COINS = new Map([
["Xrp",0.4],
["Ada",0.31],
["Shiba",0.00000914],
["Doge",0.087],
["Gala",0.025],
["Sand",0.55],
["Mana",0.39],
["FTM",0.18],
["Trx",0.05],
["Eos",0.92],
["Hbar",0.0492],
["Ftt",1.5],
["GoldenBox",0]
]);
let N = 5;
let GoldenBoxValue = 500;
const coinPie = function(x){
return Math.pow(x, 5) * 100 * 5 +
5 * Math.pow(x, 4) * (1-x) * 20 * 4 +
10 * Math.pow(x, 3) * Math.pow(1-x,2) * 10 * 3 +
10 * Math.pow(x, 2) * Math.pow(1-x,3) * 4 * 2 +
5 * x * Math.pow(1-x,4);
}
const findCoinPie = function(a,b,t,eps=0.000001){
let c = (a + b)/2;
if (b-a)<eps:
return c;
if coinPie(c)>t{
return findCoinPie(a,c,t,eps);
}
return findCoinPie(c,b,t,eps);
}
const generateProbs = function(values, q=0.4){
n = values.length;
var probs = Array.apply(0, Array(n));
var sum_prob = 0;
var sum_out = 0;
const minValue = Math.min.apply(Math, values) + 0.000001;
for (let i = 0; i < n-1; i++) {
if (values[i] > minValue){
const v = findCoinPie(0,0.5,q/(4*n)/values[i]);
probs[i] = v;
sum_out += coinPie(v) * values[i];
sum_prob += v;
}
}
sum_out = q - sum_out;
sum_prob = 1 - sum_prob;
return {sum_out:sum_out, sum_prob:sum_prob, probs:probs};
}
const findFreeProb = function(a,b,value,sum_prob,min_price,gb_val,eps=0.000001){
var c = (a + b)/2
var p1 = c;
var p2 = sum_prob-c;
if ((b-a)<eps){
return {p1:p1,p2:p2};
}
val = Math.pow(p1,5) * gb_val + coinPie(p2) * min_price;
//#print("vc:",val,c,sum_prob-c)
if (val>value){
return findFreeProb(a,c,value,sum_prob,min_price,gb_val,eps);
}
return findFreeProb(c,b,value,sum_prob,min_price,gb_val,eps);
}
const generateRealProbs(coin_values){
let minPrice = 500;
let it =-1;
let i = 0;
for (const x of coin_values.values()) {
if(x>0 && x <minPrice){
minPrice = x;
it = i;
}
i++;
}
const {sums, sum_prob, probs } = generateProbs(coin_values.values());
const {p,p1} = findFreeProb(0,sum_prob,sums, sum_prob, min_price, GoldenBoxValue,0.000001);
probs[-1] = p;
probs[it] = p1;
return probs;
}
//https://stackoverflow.com/questions/41654006/numpy-random-choice-in-javascript
function randomChoice(p) {
let rnd = p.reduce( (a, b) => a + b ) * Math.random();
return p.findIndex( a => (rnd -= a) < 0 );
}
function randomChoices(p, count) {
return Array.from(Array(count), randomChoice.bind(null, p));
}
function generateValues(coin_values,probs){
let names = Array.from( coin_values.keys() );
let result = randomChoices(names, N, probs);
return result;
}
function calculateWin(coins, coins_values){
var resCoins = new Map();
for (const x of coins){
if (resCoins.has(x)){
resCoins[x] += 1;
}
else{
resCoins.set(x,1);
}
}
let sumWin =0;
for (const pair of fruits.entries()) {
const name = pair[0];
const val = pair[1];
if (v==1){
sumWin += coins_values[k];
}
else if(v==2){
sumWin += 4 * coins_values[k] * 2;
}
else if(v==3){
sumWin += 10 * coins_values[k] * 3;
}
else if(v==4){
sumWin += 20 * coins_values[k] * 4;
}
else if(v==5){
sumWin += 100 * coins_values[k] * 5;
if (name=="GoldenBox"){
sumWin += GoldenBoxValue;
}
}
}
return sumWin;
}
function testMonteCarlo(coin_values, n){
let winSum = 0;
let probs = generateRealProbs(coin_values);
for (let i = 0; i < n-1; i++) {
state = generateValues(coin_values,probs);
w = calculateWin(state, coin_values);
winSum += w;
}
return winSum;
}
function myFunction() {
/*let n = 5;
let w = testMonteCarlo(COINS,n);
let ratio = w/n;
let text="" + ratio;
console.log(ratio); */
document.getElementById("demo").innerHTML = "Paragraph changed.";
}
//document.getElementById("demo").innerHTML = text;
</script>
</head>
<body>
<h2>JavaScript</h2>
<p>Using script </p>
<p id="demo">A Paragraph</p>
<button type="button" onclick="myFunction()">Try it</button>
<p id="demo1"></p>
<p id="demo2"></p>
</body>
</html>