每次python都获得不同的随机

时间:2015-02-01 05:42:19

标签: python

所以程序运行良好。问题是,如果我想再次播放,它每次都会调用相同的随机数。我怎么做到这样每次我想再玩一次时我会拨打一个不同的随机数和卡?谢谢。

import random
import math
import time
import sys

Hearts = "Hearts"
Clubs = "Clubs"
Diamonds = "Diamonds"
Spades = "Spades"

number1 = [1,2,3,4,5,6,7,8,9,10]
number1 = random.choice(number1)
number2 = [1,2,3,4,5,6,7,8,9,10]
number2 = random.choice(number2)
number3 = [1,2,3,4,5,6,7,8,9,10]
number3 = random.choice(number3)

card1 = [Hearts, Clubs, Diamonds, Spades]
card1 = random.choice(card1)
card2 = [Hearts, Clubs, Diamonds, Spades]
card2 = random.choice(card2)
card3 = [Hearts, Clubs, Diamonds, Spades]
card3 = random.choice(card3)

blacklist = ["King" "Queen", "Jack"]
blacklist = random.choice(blacklist)

ace = ["Ace"]
ace = random.choice(ace)

runningtotal = number1 + number2
roundtotal = runningtotal + number3

def startup():
    print("Starting game... Your first cards are being drawn")
    round1()

def round1():
    if number1 == 10:
        print ("{} of {}".format(blacklist, card1))
        round2()
    elif number1 == 1:
        print("{} of {}".format(ace, card1))
        round2()
    else:
        print ("{} of {}".format(number1, card1))
        round2()

def round2():
    if number2 == 10:
        print ("{} of {}".format(blacklist, card2))
        round3()
    elif number2 == 1:
        print("{} of {}".format(ace, card2))
        round3()
    else:
        print ("{} of {}".format(number2, card2))
        round3()

def round3():
    startr3 = input("Would you like to be dealt another card? Your total is... {}".format(runningtotal))
    if startr3 == "yes":
        if number3 == 10:
            print("{} of {}".format(blacklist, card3))
        elif number3 == 1:
            print("{} of {}".format(ace, card3))
        else:
            print("{} of {}".format(number3, card3))
            if roundtotal >= 22:
                loose()
            else:
                print("You win!")
                again = input("Do you want to play again?")
                if again == "yes":
                    round1()
                elif again == "no":
                    endgame()

def endgame():
    print("Thankyou for playing")

def loose():
    looseg = input("Your total was above 21! Do you want to play again?")
    if looseg == "yes":
        number1()
    elif looseg == "no":
        endgame()
startup()

4 个答案:

答案 0 :(得分:3)

实际上,这里真正的问题是你只能在程序启动时选择一次你的号码。相反,您需要在每次需要时选择它们。 像这样:

def round1():
    number1 = random.randint(1, 10)
    if number1 == 10:
        print ("{} of {}".format(blacklist, card1))
        round2()
    elif number1 == 1:
        print("{} of {}".format(ace, card1))
        round2()
    else:
        print ("{} of {}".format(number1, card1))
        round2()

答案 1 :(得分:0)

在代码开头调用random.seed()方法一次可以解决问题。

答案 2 :(得分:0)

您需要使用random.seed()初始化生成器,后者将使用系统时间作为种子。

目前,您的程序每次运行时都使用相同的种子。

答案 3 :(得分:0)

一种简单的方法是在代码的这一部分(玩家输掉的部分)中调用round1()之前调用“setup”函数:

# ...
if again == "yes":
    # here
    round1()
elif again == "no":
#...

此函数需要更改您在开头定义的全局变量的值。这样,你就会有另一只手。