0

How to add weights to below code. example: if i want to add x2 for red and x5 for black so finally all colours will be x1 except red and black.

colours = {'red', 'blue', 'green', 'yellow', 'black', 'purple',
           'Brown', 'Orange', 'violet', 'gray'}

@bot.command(pass_context=True)  
async def pick(ctx):
  colours_copy = colours.copy()
  for n in [1, 2, 3]:
      cs = random.sample(colours_copy , k=n)
      colours_copy -= set(cs)
      await bot.send_message(ctx.message.channel, "{}\n".format(", ".join(cs)))

Demotry
  • 869
  • 4
  • 21
  • 40
  • The easiest way is to create a list out of your set `colours` and repeat `n` times the colours for which you want to have more weighting. But beware that you might get multiple times the same colour, so you have to take into account that as well. Also, as a side note, it would be better if you could provide a [MVCE](https://stackoverflow.com/help/mcve). – norok2 Aug 30 '18 at 07:57
  • 2
    Yes, but `random.sample()` does not provide a weighting mechanism. So you are left with some other trick to be done and what I suggested could work. Also, the MVCE comment refers to the fact that there are many *names* not defined in your code, so just copy-pasting what you have does not run... – norok2 Aug 30 '18 at 08:01
  • did you do it on a set or on a list? – norok2 Aug 30 '18 at 09:04
  • I am sorry, how did you test that? If I do `import random; colors = ['red'] * 5 + ['blue', 'green', 'yellow']; random.sample(colors, 3)` it seems to work as expected for me – norok2 Aug 30 '18 at 09:10
  • 1
    Possible duplicate of [A weighted version of random.choice](https://stackoverflow.com/questions/3679694/a-weighted-version-of-random-choice#26196078) – norok2 Aug 30 '18 at 09:20
  • 1
    Possible duplicate of [A weighted version of random.choice](https://stackoverflow.com/questions/3679694/a-weighted-version-of-random-choice) – Mad Physicist Aug 30 '18 at 14:04
  • I do not understand what you mean, but if that is not related to `random.sample()` or `numpy.random.choice()` I would suggest you to update your question to fully reflect that or ask a new question. Again, everything is so much simpler when you provide a MVCE. – norok2 Sep 03 '18 at 08:40
  • could you describe a bit more clearly what is your expected output? Also, if you don't write a MVCE for testing, I am afraid I cannot help you further. – norok2 Sep 03 '18 at 08:55

0 Answers0