我需要从response=
列表中获得多个随机答复,而不能重复。
因此,当有人输入!pick
时,机器人会从responses list
中随机选择。
example like Quiz top rank 1, 2, 3 and 4 to 10 :
when used `!pick` command
bot reply's
Top 1 'Eagle'
Top 2 'Apple'
Top 3 'Elephant'
Top 4 'Cat'
Top 5 'Bear'
Top 6 'Snake'
Top 7 'Deer'
Top 8 'Rhino'
Top 9 'Crocodile'
Top 10 'Lion'
更新
responses = [(60, ("One", "Five", "Seven")), (30, ("Two", "Four", "Six")), (10, ("Three", "Eight"))]
total = sum(w for w, vals in responses)
vals, weights = zip(*[(val, (w/len(vals))/total) for w, vals in responses for val in vals])
@bot.command(pass_context=True)
async def pick(k : int):
chosen = choice(vals, size=8, replace=False, p=weights)
a, b, c = chosen[:2], chosen[:2], chosen[:4]
embed = discord.Embed(description='\n'.join(b))
await bot.say(embed=embed)
答案 0 :(得分:1)
random.choices
可以列出一个权重列表。
responses = [(60, ("One", "Five", "Seven")), (30, ("Two", "Four", "Six")), (10, ("Three", "Eight"))]
vals, weights = zip(*[(val, w/len(vals)) for w, vals in responses for val in vals])
@bot.command()
async def pick(k : int):
if 0 <= k <= 10:
embed = discord.Embed(description='\n'.join(random.choices(vals, weights=weights, k=k)))
await bot.say(embed=embed)
这是使用numpy.random.choice
responses = [(60, ("One", "Five", "Seven")), (30, ("Two", "Four", "Six")), (10, ("Three", "Eight"))]
total = sum(w for w, vals in responses)
vals, weights = zip(*[(val, (w/len(vals))/total) for w, vals in responses for val in vals])
@bot.command(pass_context=True)
async def pick(k : int):
for x in zip(*[iter(choice(vals, size=6, replace=False, p=weights))]*2):
embed = discord.Embed(description='\n'.join(x))
await bot.say(embed=embed)
这是a,b,c版本:
@bot.command(pass_context=True)
async def pick(k : int):
chosen = choice(vals, size=8, replace=False, p=weights)
a, b, c = chosen[:2], chosen[2:4], chosen[4:]
for x in (a, b, c):
embed = discord.Embed(description='\n'.join(x))
await bot.say(embed=embed)