一、赌徒的问题

周末闲来无事,打算解决我一直思考的一个问题。假设我有100块钱,拿去赌博,每次从1块开始押,如果赢了,下次还是押一块,如果输了,下次就押两块,输了就继续翻倍,直到将手上的钱输光为止。如果以这种方式赌博,是否是稳赚不赔,如果要赢到200块,需要赌多少把。来吧,我们开始写程序验证。

二、程序验证

import random

def gamble(money):
    """
    赌博,赢到双倍,或输光,程序退出
    :param money: 进场金额
    :return:
    """
    total_money = money  # 进场的总金额
    lay = 1  # 初始下注的金额
    lay_cnt = 0  # 下注的次数
    win_cnt = 0  # 赢的次数
    lose_cnt = 0  # 输的次数
    win_lose = {1: '赢', 0: '输'}

    while True:
        initial_money = total_money
        initial_lay = lay

        win = random.choice([1, 0])  # 1表示赢,0表示输
        lay_cnt += 1

        if win:  # 如果赢了,下次还是押一块
            total_money += initial_lay
            lay = 1
            win_cnt += 1
        else:  # 如果输了,下次押2倍
            total_money -= initial_lay
            lay = 2 * lay
            if total_money <= lay:  # 如果手上的钱不够两倍,下次就押所有钱
                lay = total_money
            lose_cnt += 1

        # print('押注次数:{}, 押注前金额:{}, 押注金额:{}, 结果:{}, 还剩下:{}, 下次押注:{}'.format(
        #     lay_cnt, initial_money, initial_lay, win_lose[win], total_money, lay
        # ))

        if total_money <= 0:
            # print('输光了,赢的次数:{},输的次数:{}'.format(win_cnt, lose_cnt))
            return 0
        elif total_money >= 2*money:
            # print('赢麻了,赢的次数:{},输的次数:{}'.format(win_cnt, lose_cnt))
            return 1

def dogamble(money):
    """
    验证胜率
    :param money:
    :return:
    """
    gambles = 10000  # 赌博的次数
    wins = 0
    for cnt in range(gambles):
        result = gamble(money)
        wins += result
    win_percent = round(100 * wins/gambles, 2)
    return {money: win_percent}

if __name__ == '__main__':
    import matplotlib.pyplot as plt
    from concurrent.futures import ThreadPoolExecutor, as_completed
    x = range(100, 1000)  # 进场金额
    y = {}
    with ThreadPoolExecutor(max_workers=30) as executor:
        all_task = [executor.submit(dogamble, num) for num in x]
        for future in as_completed(all_task):
            y.update(future.result())
    y_list = [y.get(_) for _ in x]

    # 算一下平均胜率
    win_cnts = 0
    for i in y_list:
        if i > 50:
            win_cnts += 1
    avg_win_percents = round(100 * win_cnts / len(x), 3)

    plt.rcParams['figure.figsize'] = (20, 12)  # 设置图片大小
    fig, ax = plt.subplots()
    ax.plot(x, y_list)
    ax.set(xlabel='initial money', ylabel='win percent', title='gambles - avg win pencents {}%'.format(avg_win_percents))
    ax.set_xlim(100, 1000)
    ax.set_ylim(40, 60)
    plt.savefig('gambles', dpi=350)  # 设置图片分辨率

三、结果

Python – 解决赌徒问题-小白菜博客
可以看到胜率都在50%上下徘徊,并没有因为进场的钱的增多而上升,而且平均胜率还只有49.333%,也就是采用这种方式赌博并不比随机的输赢胜率高。
有兴趣的同学可以设置下,输赢到一半的进场钱就退出的胜率如何,我这里就不研究了。
好了,在我想出稳赚不赔的想法之前,还是老老实实搬砖吧。