In [13]:
%matplotlib inline
from __future__ import print_function, division
import numpy as np
from matplotlib.pylab import *

# cervene cisla
red = [1, 3, 5, 7, 9, 12,
14, 16, 18, 19, 21, 23,
25, 27, 30, 32, 34, 36]

# cierne cisla
black = [2, 4, 6, 8, 10, 11,
13, 15, 17, 20, 22, 24,
26, 28, 29, 31, 33, 35]

# funkcia hraj berie 2 parametre:
#   tosses - zoznam hodov
#   account_balance - pociatocny stav uctu
# a vracia:
#   account_balance - konecny stav uctu
#   account_history - vyvoj stavu uctu
# funkcia pouziva metodu tak ako je opisana v clanku a aplikuje
# ju na nahodny zoznam hodov
def play(tosses, account_balance):
    # account_history podrzi vsetky hodnoty uctu pocas celej hry
    account_history = [account_balance]
    # pociatocna uroven stavky je 1 euro
    bet = 1
    # stavkujeme na cervenu alebo na ciernu, zaciname na ciernej
    betting_on_red = False
    # vyhrali sme toto kolo?
    won = False
    
    # iterujeme cez vsetky nahodne hody
    for num in tosses:    
        # ak prave stavkujeme na cervenu a padla cervena, vyhravame
        if betting_on_red and num in red:
            won = True
        # ak stavkujeme na ciernu a padla cierna, tiez vyhravame
        elif not betting_on_red and num in black:
            won = True
        # inak sme prehrali
        else:
            won = False
        
        # ak sme toto kolo vyhrali
        if won:
            # pripiseme si vyhru
            account_balance += bet
            # zmenime farbu
            betting_on_red = not betting_on_red
            # resetujeme stavku na 1 euro
            bet = 1
        # ak sme prehrali
        else:
            # odpocitame si prehru
            account_balance -= bet
            # zvysime stavku na dvojnasobok
            bet *= 2
        
        # zapiseme si vysledok do historie
        account_history.append(account_balance)
        
        # ak sme klesli s uctom na alebo pod nulu, koncime
        if account_balance <= 0:
            return account_balance, account_history

    # vratime vysledky
    return account_balance, account_history

# Ak predpokladame ze clovek odohra 1 hru za 1 minutu, tak za 3 hodiny
# nahra 180 hier. Autor clanku uvadza 180-200 eur za 3 hodiny denne
# nasledujuci riadok vygeneruje 180 uniformne nahodnych cisel v rozpati
# 0 az 37, ako vo FR rulete
tosses = np.random.random_integers(0, 37, 180)

# vypiseme si najmensie a najvacsie cislo ake padlo pocas 180 tahov
print("Min number: {} Max number: {}".format(np.min(tosses), np.max(tosses)))

# spustime hru s nahodnymi hodmi a vkladom 200 eur
account_balance, account_history = play(tosses, 200)

# vypiseme si konecny stav
print("Konecny stav uctu: {}".format(account_balance))

# Nakreslime si graf priebehu
figure(figsize=(17, 5))
plot(account_history)
show()

# nakreslime si histogram cisel ake nam padali
figure(figsize=(17, 5))
hist(tosses, bins=38)
show()
Min number: 0 Max number: 36
Konecny stav uctu: -36
In [45]:
# sem budeme ukladat vysledky jednotlivych 3-hodinovych hier
results = []

# aby sme to mali cim presnejsie, zahrame si 10 000 krat po 3 hodiny
for i in range(10000):
    # vygenerujeme nahodnu hru
    tosses = np.random.random_integers(0, 37, 180)
    # zahrame
    account_balance, _ = play(tosses, 200)
    # zapiseme cisty zisk
    results.append(account_balance - 200)

# pretransformujeme na numpy array (nepodstatne)
results = np.array(results)  

# vypiseme si nejake zaujimavosti
print("Minimalna vyhra: {}".format(np.min(results)))
print("Maximalna vyhra: {}".format(np.max(results)))
print("Pravdepodobnost ze pridete o vsetko: {}".format(np.count_nonzero(results <= 0) / len(results)))

# nakreslime si grafik ako sa nam vodilo
figure(figsize=(17, 5))
scatter(np.argwhere(results > 0).ravel(), results[results > 0], color='g', marker='x')
scatter(np.argwhere(results <= 0).ravel(), results[results <= 0], color='r', marker='x')
xlim(0, len(results))
show()
Minimalna vyhra: -455
Maximalna vyhra: 109
Pravdepodobnost ze pridete o vsetko: 0.3477
In [22]:
 
In [ ]: