制作彩票,似乎我无法获胜

时间:2016-02-26 22:34:23

标签: python

这是我的代码购买,无论我运行多少次,我得到的就是你输了。代码有什么问题?

import random

# Creates a number to count the amount of plays
count =  1;
# Creates a variable to store the amount of starting money
money = 10;
# Creates a variable telling you how much money you start with
startingCash = "You start with $" + str(money) + "!";

while (count < 101):

    # Variables for the lottery numbers
    lottery1 = random.randint(1,9);
    lottery2 = random.randint(1,9);
    lottery3 = random.randint(1,9);
    lottery4 = random.randint(1,9);

    # Lottery variables in one single variable
    lotteryTotal = (lottery1, lottery2, lottery3, lottery4);

    # Variables for the drawn ticket
    drawn1 = random.randint(1,9);
    drawn2 = random.randint(1,9);
    drawn3 = random.randint(1,9);
    drawn4 = random.randint(1,9);

    # Variable for the drawn ticket in one single variable
    drawnTotal = (drawn1, drawn2, drawn3, drawn4);
    # Variable that changes the money variable so the player has 2 less dollars
    money = money - 2;

==符号似乎被忽略或行为不同。如果它们彼此相等,我希望它if

    if( drawnTotal == lotteryTotal):
        count = count + 1;
        money = money + 5;
        print ("Lottery Numbers: " + str(lotteryTotal));
        print ("Your Numbers: " + str(drawnTotal));
        print ("You Won $5!");
        input("Press Enter to continue")
    else:
        print ("Lottery Numbers: " + str(lotteryTotal));
        print ("Your Numbers: " + str(drawnTotal));
        print ("You Lost!");
        input("Press Enter to continue");

2 个答案:

答案 0 :(得分:4)

您的代码 正在运行......结果与您的预期不符。

问题在于,要获胜,您需要连续两次生成相同的随机数字序列。那概率是多少?

重复单个1-9位数的概率是9中的1。如果你有两个这样的数字,你有两个1/9概率,它们的复合概率是1/9 * 1/9,或者1/81。如果您有,就像你的情况一样,你将赢得一次1/9 * 1/9 * 1/9 * 1/9 = 1/6561游戏。

你尝试了“很多次”,但是......你尝试足够次吗?即使在一千场比赛之后,至少赢得一次的概率也不到15%。即使在6561场比赛之后,至少赢得一次的概率也远不及100% - 实际上它接近三分之二。

我修改它只告诉我胜利的数量,并且只有当你确实获胜时才这样做(MarkyPython的建议)。过了一段时间,它告诉我,

100 won in 686114 games; win rate is 95% of expected.

(顺便说一句,第一场胜利是在29172场比赛之后)。

答案 1 :(得分:0)

很好的解释lserni。

Khodexian,如果您愿意增加获胜机会的数量,您可以简单地考虑数字并忽略它们的生成顺序。

例如,

import random

# Creates a number to count the amount of plays
count =  0;
# Creates a variable to store the amount of starting money
money = 10;
# Creates a variable telling you how much money you start with
startingCash = "You start with $" + str(money) + "!";
win, lost = 0, 0
while (win < 100):

    count = count + 1;

    # Variables for the lottery numbers
    lotteryTotal = [random.randint(1,9), random.randint(1,9), random.randint(1,9), random.randint(1,9)]

    # Variable for the drawn ticket in one single variable
    drawnTotal = [random.randint(1,9), random.randint(1,9), random.randint(1,9), random.randint(1,9)]

    # Variable that changes the money variable so the player has 2 less dollars
    money = money - 2;

    if( sorted(lotteryTotal) == sorted(drawnTotal) ):
        money = money + 5;
        print ("Lottery Numbers: " + str(lotteryTotal));
        print ("Your Numbers: " + str(drawnTotal));
        print ("You Won $5!");
        win += 1
    else:
        print ("Lottery Numbers: " + str(lotteryTotal));
        print ("Your Numbers: " + str(drawnTotal));
        print ("You Lost!");
        lost += 1

print ('played {}, won {} and lost {}'.format(count, win, lost))

我得到这样的统计数据,

played 40170, won 100 and lost 40070