为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
噜噜哒 2023-11-09 16:52:01
因此,对于我正在编写的这段代码,我试图为三个不同的玩家生成一组三张随机卡,其中一个是人类,两个是模拟的。为此,我正在使用这段代码。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 仍然相同。此外,即使在使用此代码在三组之间进行随机交易的第四个代码块之后
查看完整描述

2 回答

?
aluckdog

TA贡献1847条经验 获得超7个赞

如果你有Python 3.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)


查看完整回答
反对 回复 2023-11-09
?
翻翻过去那场雪

TA贡献2065条经验 获得超13个赞

看来您复制了以前的代码块,并且该return部分保持不变。:) 尝试尽可能地编写 DRY 代码。你也可以这样做:

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


查看完整回答
反对 回复 2023-11-09
  • 2 回答
  • 0 关注
  • 68 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信