python随机函数为两个不同的实例生成相同的值

python随机函数为两个不同的实例生成相同的值,python,random,Python,Random,因此,对于我正在编写的代码,我试图为三个不同的玩家生成一组三张随机卡片,其中一张是人,两张是模拟的。为此,我使用此代码 def shuffle_p1(): p_1_human_card_1=random.randint(1,3) p_1_human_card_2=random.randint(1,3) p_1_human_card_3=random.randint(1,3) p_1_human_cards=[p_1_human_card_1,p_1_human_c

因此,对于我正在编写的代码,我试图为三个不同的玩家生成一组三张随机卡片,其中一张是人,两张是模拟的。为此,我使用此代码

def shuffle_p1():
    p_1_human_card_1=random.randint(1,3)
    p_1_human_card_2=random.randint(1,3)
    p_1_human_card_3=random.randint(1,3)
    p_1_human_cards=[p_1_human_card_1,p_1_human_card_2,p_1_human_card_3]

    return (p_1_human_cards)


def shuffle_p2():
    p_2_ai_card_1=random.randint(1,3)
    p_2_ai_card_2=random.randint(1,3)
    p_2_ai_card_3=random.randint(1,3)
    p_2_ai_cards=[p_2_ai_card_1,p_2_ai_card_2,p_2_ai_card_3]

    return (p_1_human_cards)


def shuffle_p3():
    p_3_ai_card_1=random.randint(1,3)
    p_3_ai_card_2=random.randint(1,3)
    p_3_ai_card_3=random.randint(1,3)
    p_3_ai_cards=[p_3_ai_card_1,p_3_ai_card_2,p_3_ai_card_3]

    return (p_1_human_cards)

p_1_human_cards=shuffle_p1()
p_2_ai_cards=shuffle_p2()
p_3_ai_cards=shuffle_p3()
这将生成三张牌,但玩家1和玩家2每次都有完全相同的牌。我甚至放了一套不同的代码

def card_auth_p1():
    while p_1_human_cards[0]==p_1_human_cards[1] or p_1_human_cards[0]==p_1_human_cards[2]:
        p_1_human_cards[0]=random.randint(1,3)
    while p_1_human_cards[1]==p_1_human_cards[0] or p_1_human_cards[1]==p_1_human_cards[2]:
        p_1_human_cards[1]=random.randint(1,3)

def card_auth_p2():
    while p_2_ai_cards[0]==p_2_ai_cards[1] or p_2_ai_cards[0]==p_2_ai_cards[2]:
        p_2_ai_cards[0]=random.randint(1,3)
    while p_2_ai_cards[1]==p_2_ai_cards[0] or p_2_ai_cards[1]==p_2_ai_cards[2]:
        p_2_ai_cards[1]=random.randint(1,3)

def card_auth_p3():
    while p_3_ai_cards[0]==p_3_ai_cards[1] or p_3_ai_cards[0]==p_3_ai_cards[2]:
        p_3_ai_cards[0]=random.randint(1,3)
    while p_3_ai_cards[1]==p_3_ai_cards[0] or p_3_ai_cards[1]==p_3_ai_cards[2]:
        p_3_ai_cards[1]=random.randint(1,3)
而且

if p_1_human_cards == p_2_ai_cards or p_1_human_cards == p_3_ai_cards:
    p_1_human_cards=shuffle_p1()

if p_2_ai_cards == p_1_human_cards or p_2_ai_cards == p_3_ai_cards:
    p_2_ai_cards=shuffle_p2()

if p_3_ai_cards == p_1_human_cards or p_2_ai_cards == p_3_ai_cards:
    p_3_ai_cards=shuffle_p3()
为了确保它们不完全相同,但打印这三个列表表明玩家1和2仍然是相同的。此外,即使在第四块代码之后,在三盘之间随机交易也使用此代码

def card_trade():
    print('Round 1')
    p_1_choice=int(input('pick the card you want to take from player 2'))
    while p_1_choice<1 or p_1_choice>3:
        print('pick between 1 and 3')
        p_1_choice=int(input('pick the card you want to take from player 2'))

    if p_1_choice==1:
       p_1_extra_card=p_2_ai_cards[0]
       p_2_ai_cards.remove(p_2_ai_cards[0])

    elif p_1_choice==2:
       p_1_extra_card=p_2_ai_cards[1]
       p_2_ai_cards.remove(p_2_ai_cards[1])

    elif p_1_choice==3:
       p_1_extra_card=p_2_ai_cards[2]
       p_2_ai_cards.remove(p_2_ai_cards[2])

    p_1_human_cards.append(p_1_extra_card)

    p_2_choice=random.randint(1,3)

    print('AI decided to take',p_2_choice,'from player 3')
    if p_2_choice==1:
       p_2_extra_card=p_3_ai_cards[0]
       p_3_ai_cards.remove(p_3_ai_cards[0])

    elif p_2_choice==2:
       p_2_extra_card=p_3_ai_cards[1]
       p_3_ai_cards.remove(p_3_ai_cards[1])

    elif p_2_choice==3:
       p_2_extra_card=p_3_ai_cards[2]
       p_3_ai_cards.remove(p_3_ai_cards[2])

    p_2_ai_cards.append(p_2_extra_card)

    p_3_choice=random.randint(1,4)

    print('AI decided to take',p_3_choice,'from player 1')
    if p_3_choice==1:
       p_3_extra_card=p_1_human_cards[0]
       p_1_human_cards.remove(p_1_human_cards[0])

    elif p_3_choice==2:
       p_3_extra_card=p_1_human_cards[1]
       p_1_human_cards.remove(p_1_human_cards[1])

    elif p_3_choice==3:
       p_3_extra_card=p_1_human_cards[2]
       p_1_human_cards.remove(p_1_human_cards[2])

    elif p_3_choice==3:
       p_3_extra_card=p_1_human_cards[3]
       p_1_human_cards.remove(p_1_human_cards[33])

    p_3_ai_cards.append(p_3_extra_card)
def卡交易():
打印(“第1轮”)
p_1_choice=int(输入('从玩家2中选择你想要的牌')
而p_1_选择3:
打印('在1和3'之间选择)
p_1_choice=int(输入('从玩家2中选择你想要的牌')
如果p_1_choice==1:
p_1_额外卡=p_2_ai_卡[0]
p_2_ai_卡。移除(p_2_ai_卡[0])
elif p_1_选项==2:
p_1_额外卡=p_2_ai_卡[1]
p_2_ai_卡。移除(p_2_ai_卡[1])
elif p_1_选项==3:
p_1_额外卡=p_2_ai_卡[2]
p_2_ai_卡。移除(p_2_ai_卡[2])
p_1_人卡。附加(p_1_额外卡)
p_2_choice=random.randint(1,3)
打印('AI决定接受',p_2_选择,'from player 3')
如果p_2_choice==1:
p_2_额外卡=p_3_ai_卡[0]
p_3_ai_卡。移除(p_3_ai_卡[0])
elif p_2_choice==2:
p_2_额外卡=p_3_ai_卡[1]
p_3_ai_卡。移除(p_3_ai_卡[1])
elif p_2_选项==3:
p_2_额外卡=p_3_ai_卡[2]
p_3_ai_卡。移除(p_3_ai_卡[2])
p_2_ai_卡。附加(p_2_额外卡)
p_3_choice=random.randint(1,4)
打印('AI决定接受',p_3_选择,'from player 1')
如果p_3_choice==1:
p_3_额外卡=p_1_人类卡[0]
p_1_人卡。移除(p_1_人卡[0])
elif p_3_选项==2:
p_3_额外卡=p_1_人类卡[1]
p_1_人_卡。移除(p_1_人_卡[1])
elif p_3_选项==3:
p_3_额外卡=p_1_人类卡[2]
p_1_人_卡。移除(p_1_人_卡[2])
elif p_3_选项==3:
p_3_额外卡=p_1_人类卡[3]
p_1_人_卡。移除(p_1_人_卡[33])
p_3_ai_卡。附加(p_3_额外卡)
前两名球员的结局总是完全一样。我似乎无法理解为什么他们似乎永远不会改变,如果有任何见解可以帮助解决这个问题,我将不胜感激


另一件需要注意的是,有时在卡片交易过程中()当人类玩家选择3作为他们的选择时,计算机可能会抛出一个错误,但这一点非常不一致,因此我也无法确定该错误的来源。

似乎您复制了以前的代码块,并且返回的部分保持不变。。)试着尽可能地去做。 您也可以这样做:

import random
def shuffle_p1():
    p_1_human_cards=[random.randint(1,3) for _ in range(3)]
    return p_1_human_cards

def shuffle_p2():
    p_2_ai_cards=[random.randint(1,3) for _ in range(3)]
    return p_2_ai_cards

def shuffle_p3():
    p_3_ai_cards=[random.randint(1,3) for _ in range(3)]
    return p_3_ai_cards

如果您有Python3.6+,也可以尝试使用secrets

try:
    import secrets as random
except ModuleNotFoundError:
    import random

# Use system resources for randomization
rnd = random.SystemRandom()

# Set available card options
AVAILABLE_CARDS = [1,2,3,]

# Set number of cards per hand.
NUMBER_OF_CARDS = 3

# Create a simple key-value collection.
players = dict(
    p_1_human_cards = None,
    p_2_ai_cards = None,
    p_3_ai_cards= None,
    )

# Define a shuffler() function.  Parameters are number of cards and available cards.
def shuffler(n_cards=NUMBER_OF_CARDS , available_cards = AVAILABLE_CARDS ):
    return tuple([rnd.choice(available_cards) for _ in range(n_cards)])

# Iterate through players to set each hand
for hand in players:
    players[hand] = shuffler()


# Print result
for player, hand in players.items():
    print(f"{player}: {hand}")
输出:

p_1_human_cards: (2, 1, 3)
p_2_ai_cards: (3, 2, 1)
p_3_ai_cards: (2, 3, 3)

为什么
shuffle\u p2
返回
(p\u 1\u人类卡)
?这发生在我们所有人身上。有时你已经盯着同一个代码看了很长时间,你看不到明显的错误。但对于在重新运行它并更改为shuffle\u p2和3之后的最后错误,它仍然会给出这个错误UnboundLocalError:在赋值之前引用局部变量“p\u 3\u extra\u card”。请提供更多帮助?@MustafaShahzad:您测试
elif p_3_choice==3:
两次,而不是测试
elif p_3_choice==4:
(或者只使用
else:
,这在这里是有意义的);如果出现
4
,您永远不会分配
p_3_额外卡
,因此会出现错误。@ShadowRanger感谢您指出这一点。我想我今天已经在这方面工作太久了,如果这就是我正在犯的错误类型的话。希望明天我能够在不犯错误的情况下完善这个函数。它是同一个函数写了三次。是的,我承认我从不同的代码文件中复制了一些东西,用于今天早上我正在处理的同一个任务,但有点太乱,无法清理,所以我重新开始。但是我会把你的建议牢记在心,谢谢你,先生。@DYZ是的,那不是最好的。只是稍微清楚一点。也许你可以建议编辑一下。