赌徒等价殃框架搭建及完整代码(加入蒙特卡洛模拟)

搭建基本框架

import random 
import matplotlib.pyplot as plt

def give_the_number():
    number=random.randint(1,100)
    
    if number<=50:
#        print(number,'The number is 1-50. You lose.Play again')
        return False
    else:
#        print(number,'The number is 51-100.You win.Play more to become
#Gaofushuai')
        return True

#等价yang加仓

def a_mart_fool(funds,initital_wager,game_count):
    wager=inintial_wager
    x=[]
    y=[]
    count_game_count=1
    previous_game='win'
    previous_game_wager=initial_wager
    
    while current_game_count<=game_count:
        if previous_game=='win':
            if give_the_number():
            
            else:
        
    elif previous_game=='loss':
        if give_the_number():
            
        elif:
            
        current_game_count+=1  

完整代码:

import random 
import matplotlib.pyplot as plt

def give_the_number():
    number=random.randint(1,100)
    
    if number<=50:
#        print(number,'The number is 1-50. You lose.Play again')
        return False
    else:
#        print(number,'The number is 51-100.You win.Play more to become
#Gaofushuai')
        return True

#等价yang加仓:
def a_smart_fool(funds,initial_wager,game_count):
    wager=initial_wager
    x=[]
    y=[]
    current_game_count=1
    previous_game='win'  #设置默认第一轮是win的,否则后续无法加钱
    previous_game_wager=initial_wager
    
    while current_game_count<=game_count:
        if previous_game=='win':
            print('The smart win the last~Play more')
            if give_the_number():  #win的情况下,下一轮要怎么样
                funds+=wager
                print(funds)
                x.append(current_game_count)
                y.append(funds)
                    
            else:
                funds-=wager
                previous_game='loss'
                print(funds)
                previous_game_wager=wager
                x.append(current_game_count)
                y.append(funds)
                
                if funds<0: #如果剩余的钱是0时就破产了,不能继续玩
                    print('The smart fool wnet break in')
                    break
                
                
                
        
        elif previous_game=='loss':  #loss的情况下,下一轮要怎么样
            print('The smart fool lost the last one,But he is smart
and he will double up')
            if give_the_number():
                wager=previous_game_wager*2
                print('The fool won'+str(wager))
                funds=wager
                previous_game='win'
                x.append(current_game_count)
                y.append(funds)
                
                
            else:
               wager=previous_game_wager*2
               print('The fool lost'+str(wager))
               funds-=wager
               print(funds)
               previous_game='loss'
               previous_game_wager=wager
               x.append(current_game_count)
               y.append(funds)
               
               if funds<0:
                   print('The smart fool went broke in'
                         +str(current_game_count)+'games')
                   break
                
        current_game_count+=1 
    print(funds)
    plt.plot(x,y)
    
n=0    #模拟100个人
while n<100:
    a_smart_fool(10000,1000,100)
    n+=1
 
a_smart_fool(10000,1000,100)
plt.xlabel('How many games does the fool played')
plt.ylabel('Funds')           
plt.show()

运行结果:

The smart fool lost the last one,But he is smartand he will double up
The fool won32000
The smart win the last~Play more
0
The smart fool lost the last one,But he is smartand he will double up
The fool won64000
The smart win the last~Play more
0
The smart fool lost the last one,But he is smartand he will double up
The fool won128000
The smart win the last~Play more
256000
The smart win the last~Play more
128000
The smart fool lost the last one,But he is smartand he will double up
The fool lost256000
-128000
The smart fool went broke in22games
-128000

原文地址:https://www.cnblogs.com/jpr-ok/p/12462203.html