packages = ["numpy" ]
import numpy as np
def calc_prob(budget, target):
if budget >= target: return 1
p = 12/38
q = 1 - p
b = np.zeros(target - 1)
A = -np.identity(target-1)
for pocket in range(1, target):
row = pocket - 1
bet = 1
chance = 1
while pocket:
while bet <= pocket:
if pocket + bet*2 < target: A[row, pocket + bet*2 - 1] += chance * p
else: b[row] -= chance * p
chance *= q
pocket -= bet
bet *= 2
bet = 1
bb = np.linalg.solve(A, b)
return bb[budget - 1]
def calc_prob2(budget, target):
if budget >= target: return 1
p = 18/38
q = 1 - p
b = np.zeros(target - 1)
A = -np.identity(target-1)
for pocket in range(1, target):
row = pocket - 1
bet = 1
chance = 1
while pocket:
while bet <= pocket:
if pocket + bet < target: A[row, pocket + bet - 1] += chance * p
else: b[row] -= chance * p
chance *= q
pocket -= bet
bet *= 2
bet = 1
bb = np.linalg.solve(A, b)
return bb[budget - 1]
def find_best(budget, target):
return {"Aggressive": calc_prob(budget, target) * 100,
"Safe": calc_prob2(budget, target) * 100}
def handle_enter_click():
budget = int(Element("budget_input").value) if Element("budget_input").value else 0
target = int(Element("target_input").value) if Element("target_input").value else 0
result = find_best(budget, target)
Element("aggressive_value").element.innerHTML = "%.2f%%" % result['Aggressive']
Element("safe_value").element.innerHTML = "%.2f%%" % result['Safe']
if result['Aggressive'] > result['Safe']:
Element("aggressive_value").element.classList.add("font-bold")
Element("safe_value").element.classList.remove("font-bold")
else:
Element("aggressive_value").element.classList.remove("font-bold")
Element("safe_value").element.classList.add("font-bold")
Roulette Strategy App