zoukankan      html  css  js  c++  java
  • 赌徒等价殃框架搭建及完整代码(加入蒙特卡洛模拟)

    搭建基本框架

    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

  • 相关阅读:
    Thinkphp无法加载验证码 undefined function Thinkimagettftext() 解决方案 mac系统
    fragment使用不当 导致java.lang.IllegalStateException
    怎样从GitHub项目中,下载单个文件夹或文件
    Android 5.0 app样式
    OpenCV fitline直线拟合函数学习
    Linux文件系统
    Ubuntu 12.04 笔记本触摸板失灵解决方法
    利用微软测试工具PICT生成测试用例
    Myeclipse 使用JUnit 进行单元测试
    Myeclipse 2014 安装checkstyle、PMD和Metrics
  • 原文地址:https://www.cnblogs.com/jpr-ok/p/12462203.html
Copyright © 2011-2022 走看看