1

I have this game where you need to react with the 5 random emojis sent from a list. The problem is that sometimes random.randint() spits out the same emoji twice so its impossible react twice to the same message with the same emoji. Is there a better way of doing multiple 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))

1 Answers1

3

Random numbers can, by definition, be repetitive as any call to randint is independent of the previous one. You can replace the following:

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)]

with this:

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

From the docs (emphasis mine):

random.sample(population, k)

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

That being said, it is a better idea to refactor that part and switch to using a list instead of declaring 5 variables (it would've been messier if you needed 50, or 500).

Community
  • 1
  • 1
Selcuk
  • 57,004
  • 12
  • 102
  • 110
  • Makes sense. I haven't used random.sample so this will come in handy. Thanks again for the suggestion! –  Feb 22 '19 at 06:16