python - 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()
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)
前两名球员的结局总是完全相同。我似乎无法弄清楚为什么它们似乎永远不会改变,并且希望有任何见解来帮助解决这个问题。
在纸牌交易()过程中有时需要注意的另一件事是,当人类玩家选择 3 作为他们的选择时,计算机可能会抛出错误,但它的做法非常不一致,因此我也无法查明该错误的根源。
解决方案
如果你有 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)
推荐阅读
- java - 将Java中两个不均匀数组中的每个元素相加
- java - 会话参数为空
- wordpress - 如何使用子主题functions.php只为WordPress中的博客重写自定义标题和描述
- hyperledger-fabric - 是否需要为 hyperledger-fabric 创建外部 AngularJS/JSP/JSF
- django - Django中单个表单上的多个提交按钮
- arrays - 从字节切片中提取位到 int 切片中
- tsql - 为什么 TRY_CAST 仅适用于某些记录
- java - 在没有 maven 的情况下构建 unirest 请求
- angular - MarkAsTouched 对角度 4 中的单选按钮(不显示红色单选按钮)没有影响
- mysql - 具有内部变量的视图