gpt4 book ai didi

python - Random.Randint() 重复

转载 作者:太空宇宙 更新时间:2023-11-04 09:33:16 26 4
gpt4 key购买 nike

我有这个游戏,您需要对列表中发送的 5 个随机表情符号使用react。问题在于,有时 random.randint() 会吐出相同的表情符号两次,因此不可能用相同的表情符号对同一消息做出两次 react 。有没有更好的方法来执行多个 random.randints?

async def food_loop():
await client.wait_until_ready()
channel = client.get_channel("523262029440483329")
while not client.is_closed:
foodtime = random.randint(1440, 1880)
food = ['🍇','🍈','🍉','🍊','🍋','🍌','🍍','🍎','🍏','🍐','🍑','🍒','🍓','🥝','🍅','🥑','🍆','🥔','🥕','🌽','🌶',
'🥒','🍄','🥜','🌰','🍞','🥐','🥖','🥞','🧀','🍖','🍗','🥓','🍔','🍟','🍕','🌭','🌮','🌯',
'🥙','🍳','🥘','🍲','🥗','🍿','🍱','🍘','🍙','🍚','🍛','🍜','🍝','🍠','🍢','🍣','🍤','🍥','🍡',
'🍦','🍧','🍨','🍩','🍪','🎂','🍰','🍫','🍬','🍭','🍮','🥛','☕','🍵','🍶','🍾','🍷','🍸','🍹','🍺',
'🥃']
food1 = food[random.randint(0,79)]
food2 = food[random.randint(0,79)]
food3 = food[random.randint(0,79)]
food4 = food[random.randint(0,79)]
food5 = food[random.randint(0,79)]
foodmonies = random.randint(350,750)
up = 'order up'
def orderup(m):
return m.content.lower() == up
foodmsg = 'Customer has ordered {}, {}, {}, {}, and {}! Fulfill their order ASAP!'.format(food1, food2, food3, food4, food5)
foodmsgsend = await client.send_message(channel, foodmsg)
foodpay1 = await client.wait_for_reaction(emoji=food1, message=foodmsgsend, timeout=3600,
check=lambda reaction, user: user != client.user)
foodpay2 = await client.wait_for_reaction(emoji=food2, message=foodmsgsend, timeout=3600,
check=lambda reaction, user: user != client.user)
foodpay3 = await client.wait_for_reaction(emoji=food3, message=foodmsgsend, timeout=3600,
check=lambda reaction, user: user != client.user)
foodpay4 = await client.wait_for_reaction(emoji=food4, message=foodmsgsend, timeout=3600,
check=lambda reaction, user: user != client.user)
foodpay5 = await client.wait_for_reaction(emoji=food5, message=foodmsgsend, timeout=3600,
check=lambda reaction, user: user != client.user)
foodguess = await client.wait_for_message(timeout=3600, channel=channel, check=orderup)
if foodpay1 and foodpay2 and foodpay3 and foodpay4 and foodpay5 and foodpay3.user.id in blacklist:
pass
else:
if foodpay1 and foodpay2 and foodpay3 and foodpay4 and foodpay5 and foodguess:
await client.delete_message(foodmsgsend)
await client.send_message(channel, "{} fulfills the order and earns ${}".format(foodpay5.user.mention, foodmonies))
add_dollars(foodpay5.user, foodmonies)
await asyncio.sleep(int(foodtime))

最佳答案

根据定义,随机数可以是重复的,因为对 randint 的任何调用都独立于前一个。您可以替换以下内容:

food1 = food[random.randint(0,79)]
food2 = food[random.randint(0,79)]
food3 = food[random.randint(0,79)]
food4 = food[random.randint(0,79)]
food5 = food[random.randint(0,79)]

用这个:

food1, food2, food3, food4, food5 = random.sample(food, 5)

来自docs (强调我的):

random.sample(population, k)

Return a k length list of unique elements chosen from the population sequence or set.

话虽如此,重构该部分并改用列表而不是声明 5 个变量(如果您需要 50 或 500 个变量会更加困惑)是一个更好的主意。

关于python - Random.Randint() 重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54797202/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com