Random.Randint() 重复
Posted
技术标签:
【中文标题】Random.Randint() 重复【英文标题】:Random.Randint() Repeating 【发布时间】:2019-07-14 18:48:58 【问题描述】:我有这个游戏,你需要对从列表中发送的 5 个随机表情符号做出反应。问题在于,有时random.randint()
会两次吐出相同的表情符号,因此不可能对具有相同表情符号的相同消息做出两次反应。有没有更好的方法来做多个 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))
【问题讨论】:
可能与以下内容重复:***.com/questions/1262955/… 【参考方案1】:根据定义,随机数可以重复,因为对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)
返回从种群序列或集合中选择的唯一元素的 k 长度列表。
话虽如此,最好重构该部分并切换到使用列表而不是声明 5 个变量(如果需要 50 个或 500 个变量,那就更麻烦了)。
【讨论】:
有道理。我没有使用 random.sample 所以这会派上用场。再次感谢您的建议!以上是关于Random.Randint() 重复的主要内容,如果未能解决你的问题,请参考以下文章
用python语言。掷四个骰子,并记录其相应在点数之和。重复100次,展示每个结果相应在次数和。
random.randint()与np.random.randint()的区别