鞅方法

时间:2016-06-25 01:45:45

标签: python

我试图模拟martingale method,但它似乎没有做任何事情。

这是我的代码:

import random

w = 1  # wager
# w1 = w  # an amount for reference when the wager has to go back to its original value
w = int(w)

br = 10000000  # Bankroll
# br1 = br  # Reference amount like w1
br = int(br)

losses = 0  # Keeps track of losses in loop
losses = int(losses)

wins = 0  # Keeps track of wins in loop
wins = int(wins)

b = 0  # Amount of bankruptcies that occur during the loop
b = int(b)

i = 1

boole = 0

x = 1
while x <= 1000:

    while i <= 100000:
        y = random.randint(0, 1)  # Picking between 1 or 0 for a %50 chance.
        if y == 1:  # If you win, you add the wager to the bankroll, add one to the wins, and set the wager to its beginning value
            br += w
            wins += 1
            w = 1

        else:
            br -= w  # If you lose, you subtract the wager from the bankroll, double the wager, and add a loss.
            w *= 2
            losses += 1

        if br <= 0:  # If the bankroll reaches 0 or below, it restarts adds to the bankruptcies variable
            b += 1
            break

        if boole == 1:
            w *= 2
            boole = 0

    i += 1  # Continues the loop
x += 1

print("Wins:", wins, "\nLosses:", losses, "\nBankruptcies:", b, "\nNet Gain:", )  # Stating info

由于某种原因它只是不起作用。我想要它输出东西,但它没有。

编辑:如果你们没有把我最初的想法偏离到我不理解的东西,我会很感激。

我基本上希望它输出我赢得50/50的次数和丢失的次数。还有多少次我在x循环中破了1000多次。以及我从所有事情中获得的净收益。

3 个答案:

答案 0 :(得分:0)

尝试使用iterator:

for x in  range(0,10):

   for i in range(0,100):

        y = random.randint(0, 1)  # Picking between 1 or 0 for a %50 chance.
        if y == 1:  #
            br += w
            wins += 1
            w = 1

        else:
            br -= w  # 
        w *= 2
        losses += 1

        if br <= 0:  # 

            b += 1
            break

        if boole == 1:
            w *= 2
            boole = 0


print("Wins:", wins, "\nLosses:", losses, "\nBankruptcies:", b, "\nNet Gain:", ) #

答案 1 :(得分:0)

它永远不会结束的原因是因为你的缩进对于递增计数器是错误的。你还有很多不必要的代码。这是一个已经完成的清理版本 - 所以你可以继续使用它。

import random

w = 1          # Wager
br = 10000000  # Bankroll
losses = 0     # Keeps track of losses in loop
wins = 0       # Keeps track of wins in loop
b = 0          # Amount of bankruptcies that occur during the loop
ng = 0         # Net gain
i = 1
x = 1
boole = 0

while x <= 1000:
    while i <= 100000:
        y = random.randint(0, 1)  # Picking between 1 or 0 for a %50 chance.
        if y == 1:   # If you win, you add the wager to the bankroll, add one to
                     # the wins, and set the wager to its beginning value
            br += w
            wins += 1
            w = 1
        else:
            br -= w   # If you lose, you subtract the wager from the bankroll,
                      # double the wager, and add a loss.
            w *= 2
            losses += 1

        if br <= 0:   # If the bankroll reaches 0 or below, it restarts adds to
                      # the bankruptcies variable
            b += 1
            break

        if boole == 1:  # Will never be True (nothing changes boole...)
            w *= 2
            boole = 0

        i += 1  # Increment the inner counter
    x += 1  # Increment the outer counter

print("Wins:", wins,
      "\nLosses:", losses,
      "\nBankruptcies:", b,
      "\nNet Gain:", ng)

答案 2 :(得分:0)

尝试使用增量符i + = 1和x + = 1进行选项卡。它们应该与循环中的其他代码对齐,而不是与while语句对齐。