上文最后几个“聪明的”的赌徒研究出一种理论上稳赚不赔的方法。
关于赌徒们提到的这个方法在概率论中叫做‘鞅’。
鞅的原名 martingale 原指一类于18世纪流行于法国的投注策略,称为加倍赌注法。在博弈中,赌徒会掷硬币,若硬币正面向上,赌徒会赢得赌本,若硬币反面向上,赌徒会输掉赌本。这一策略使赌徒在输钱后加倍赌金投注,为的是在初次赢钱时赢回之前输掉的所有钱,同时又能另外赢得与最初赌本等值的收益。当赌徒的财产和可用时间同时接近无穷时,他掷硬币后硬币正面向上的概率会接近1,由此看来,加倍赌注法似乎是一种必然能赢钱的策略 https://zh.wiki.6bu6.xyz/wiki/鞅_(概率论)
首先游戏基本规则不变,定义骰子的游戏规则:
import randomdef give_the_number():number=random.randint(1,6)if number <=3:# print("骰子的大小为:",number," 你赢了!")return Trueelse:# print("骰子的大小为:",number," 你输了!")return False
接下来讨论第一轮投掷的情况:
def smart_gambler(funds,initial_wager,game_count): #本金,初始赌注,游戏次数wager = initial_wagerx = []y = []current_game_count = 1previous_game = 'win'previous_game_wager = initial_wagerwhile current_game_count <= game_count:if previous_game == 'win': #第一轮获胜的情况print('The gambler win the last game,Play more')if give_the_number():funds += wagerprint(funds)x.append(current_game_count)y.append(funds)else: #第一轮输掉的情况funds -= wagerprevious_game = 'loss'print(funds)previous_game_wager = wagerx.append(current_game_count)y.append(funds)if funds < 0: #破产退出游戏print ('The gambler went broke in'+str(current_game_count)+'games')break
继续该讨论第二轮以至后续的情况:
elif previous_game == 'loss': #第一轮输掉了但是第二轮赢了print("we lost the last one, but the gambler will double up˜")if give_the_number():wager = previous_game_wager * 2print ('we won'+str(wager))funds += wager #增加双倍赌注print (funds)wager = initial_wagerprevious_game = 'win'x.append(current_game_count)y.append(funds)else: #第一轮输掉了,第二轮又输了wager = previous_game_wager * 2print ('we lost'+str(wager))funds -= wager #减少双倍赌注if funds < 0:print('The gambler went broke in'+str(current_game_count)+'games')breakprint(funds)previous_game = 'loss'previous_game_wager = wagerx.append(current_game_count)y.append(funds)if funds < 0: #输破产,不让玩了print('went broke after'+str(current_game_count)+'games')breakcurrent_game_count += 1print(funds)
接着我们导入可视化包,同时导入蒙特卡洛模型:
import matplotlib.pyplot as pltplt.plot(x,y)n=0while n<100:smart_gambler(10000,1000,100)n+=1plt.xlabel("How many games does the fool played")plt.ylabel("Funds")plt.show()

参数不变,还是50个赌徒,本金为10000元,赌注为1000元,进行100次模拟游戏。让我们来看下结果:

这个结果是不是与想象中的不太一样?如果以10000元为基数,可以看到有很多人都赚了钱,难道是模拟次数太少了?我们把模拟次数更改为1000,继续输出结果:

可以看到还是有少数人走到了最后,但是这个数量相比于基数50也是比较多了,根本算不上十赌九输呀,那么问题出在哪里了呢?不知道大家注意到了没有,到了200次之后有很多赌徒一把输到破产,但是下一轮又赢了回来,问题就出在这里,本金都不够下一轮赌注了还怎么赌?所以我们要考虑当投注金额大于本金时,应当停止投注,否则的话,会出现负数,与实际情况不符,所以改进了一下代码:
import randomimport matplotlib.pyplot as pltdef give_the_number():number=random.randint(1,6)if number <=3:# print("骰子的大小为:",number," 你赢了!")return Trueelse:# print("骰子的大小为:",number," 你输了!")return Falsedef smart_gambler(funds,initial_wager,game_count):current_game_count =1wager =initial_wagerprevious_game_wager=initial_wagerprevious_game =random.randint(0,1)x=[]y=[]while current_game_count<=game_count:if previous_game==1:print('The gambler win the last game,Play more')if give_the_number():funds +=wagerprint(current_game_count,wager,funds)x.append(current_game_count)y.append(funds)else:funds -=wagerprevious_game=0print(current_game_count,wager,funds)previous_game_wager=min(wager,funds) #考虑本金小于赌注的情况x.append(current_game_count)y.append(funds)if funds <=0:print('The fool went broke in '+str(current_game_count)+'games')breakelif previous_game==0:print('we lost the last game,but the gambler will double up')if give_the_number():wager =min(previous_game_wager *2,funds) #考虑本金小于赌注的情况print('we won'+str(wager))funds +=wagerprint(current_game_count,wager,funds)previous_game=1x.append(current_game_count)y.append(funds)else:wager =min(previous_game_wager *2,funds) #考虑本金小于赌注的情况print('we lost'+str(wager))funds -=wagerif funds<=0:print('The gambler went brok in '+str(current_game_count)+'games')breakprint(current_game_count,funds)previous_game=0previous_game_wager=wagerx.append(current_game_count)y.append(funds)if funds<=0:print('We went borke after'+str(current_game_count)+'games')current_game_count +=1print(current_game_count,wager,funds)plt.plot(x,y)n=0while n<100:smart_gambler(10000,1000,100)n+=1plt.xlabel('how many games does the fool play')plt.ylabel('funds')plt.show()

这次我们可以看到只有两个人坚持到最后,但是已经他们没有足够的资金进行下一轮的赌注了。
文章转载自数据信息化,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




