每次获得不同的随机 python

  • 本文关键字:随机 python 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()

实际上,这里真正的问题是,当程序启动时,您只选择一次数字。相反,您需要在每次需要它们时选择它们。喜欢这个:

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()

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

你需要使用

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

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

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

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

此函数需要更改您在开始时定义的全局变量的值。这样,你就会有不同的手。

为什么不使用:

from random import randint
number1 = randint(0,10)

而不是放一个完整的列表?

最新更新