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

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


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



以确保它们并不完全相同,但打印三个列表表明玩家 1 和 2 仍然相同。此外,即使在使用此代码在三组之间进行随机交易的第四个代码块之后



噜噜哒
浏览 122回答 2
2回答

aluckdog

如果你有Python 3.6+,你也可以尝试secretstry:    import secrets as randomexcept ModuleNotFoundError:    import random# Use system resources for randomizationrnd = random.SystemRandom()# Set available card optionsAVAILABLE_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 handfor hand in players:    players[hand] = shuffler()# Print resultfor 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)

翻翻过去那场雪

看来您复制了以前的代码块,并且该return部分保持不变。:) 尝试尽可能地编写 DRY 代码。你也可以这样做:import randomdef shuffle_p1():    p_1_human_cards=[random.randint(1,3) for _ in range(3)]    return p_1_human_cardsdef shuffle_p2():    p_2_ai_cards=[random.randint(1,3) for _ in range(3)]    return p_2_ai_cardsdef shuffle_p3():    p_3_ai_cards=[random.randint(1,3) for _ in range(3)]    return p_3_ai_cards
打开App,查看更多内容
随时随地看视频慕课网APP