繁体   English   中英

Random.Randint()重复

[英]Random.Randint() Repeating

我有这个游戏,您需要在清单中发送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))

根据定义,随机数可以重复,因为对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)

文档 (重点是我的):

random.sample(population, k)

返回从总体序列或集合中选择的唯一元素的ak长度列表。

话虽如此,最好重构该部分并改用列表而不是声明5个变量(如果需要50个或500个变量,这会比较麻烦)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM