第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

python隨機(jī)函數(shù)為兩個不同的實例生成相同的值

python隨機(jī)函數(shù)為兩個不同的實例生成相同的值

PHP
嚕嚕噠 2023-11-09 16:52:01
因此,對于我正在編寫的這段代碼,我試圖為三個不同的玩家生成一組三張隨機(jī)卡,其中一個是人類,兩個是模擬的。為此,我正在使用這段代碼。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 仍然相同。此外,即使在使用此代碼在三組之間進(jìn)行隨機(jī)交易的第四個代碼塊之后
查看完整描述

2 回答

?
aluckdog

TA貢獻(xiàn)1847條經(jīng)驗 獲得超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)


查看完整回答
反對 回復(fù) 2023-11-09
?
翻翻過去那場雪

TA貢獻(xiàn)2065條經(jīng)驗 獲得超14個贊

看來您復(fù)制了以前的代碼塊,并且該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


查看完整回答
反對 回復(fù) 2023-11-09
  • 2 回答
  • 0 關(guān)注
  • 205 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號