-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmartin.ts
86 lines (67 loc) · 1.85 KB
/
martin.ts
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
// see https://docs.google.com/spreadsheets/d/1z27GEyrFVnBBZcCJ-w2QDZS9LKDiZfK2wvD02UxifzE/edit#gid=380185017
// results: { winners: 9624253, losers: 9629697, winnersPercentage: 49.99 }
const initValues = {
balance: 100,
bet: 1,
};
let { balance, bet } = initValues;
const reset = () => {
balance = 100;
bet = 1;
};
const flipACoin = () => {
return Math.random() > 0.5 ? "heads" : "tails";
};
let bestWinner = -Infinity;
let worstLoser = Infinity;
let winners = 0;
let losers = 0;
const haveMoney = () => balance > 0;
const haveDoubled = () => balance >= initValues.balance * 2;
let lastLog = Date.now();
const possiblyEndGame = () => {
if (haveMoney() && !haveDoubled()) return;
bestWinner = Math.max(bestWinner, balance);
worstLoser = Math.min(worstLoser, balance);
if (!haveMoney()) {
losers++;
} else if (haveDoubled()) {
winners++;
} else {
throw new Error("This should never happen");
}
reset();
if (Date.now() - lastLog < 100) return;
lastLog = Date.now();
console.log({
winners,
losers,
winnersPercentage: Math.round((winners / (winners + losers)) * 10000) / 100,
// bestWinner,
// worstLoser,
});
// console.log(`${winners},${losers}`); // for csv
// if (winners + losers === 10_000) {
// process.exit(0);
// }
};
const playRound = () => {
if (bet < 1) throw new Error(`You are betting ${bet}`);
if (bet > balance)
throw new Error(`You are betting ${bet} but you have ${balance}`);
if (balance <= 0) throw new Error(`You have lost with balance ${balance}`);
if (balance >= initValues.balance * 2)
throw new Error(`You have won with balance ${balance}`);
const result = flipACoin();
if (result === "heads") {
balance += bet;
bet = 1;
} else {
balance -= bet;
bet = Math.min(bet * 2, balance);
}
};
while (true) {
playRound();
possiblyEndGame();
}