I want to make a bot that sending 3 emoji but its different emoji , but when im running it , the bot took one emoji and sending a same 3 emoji, how do i make it take the different 3 emoji?
list = ['π', 'π', 'π€£', 'π', 'π', 'π
', 'π', 'π', 'π', 'π', 'π', 'π', 'π', 'π']
text = random.choice(list)
that the code
edit : can you tell me whats wrong in this one?
import discord
import requests
import random
import sys
token = sys.argv[1]
chan = sys.argv[2]
client = discord.Client()
list = requests.get('emoji.txt').text.split("\n")
@client.event
async def on_ready():
txtchan = client.get_channel(int(chan))
while not client.is_closed():
message = ''
for x in range(5):
message += random.choice(list)
await txtchan.send(message)
client.run(token, bot=False)
4 Answers 4
The correct code for this will be
text = random.sample(list, 3)
This will randomly sample 3 emojis. Change the number to how many samples you want to draw.
-
it does prevent repetition. Every time you run this code, the output will not contain repetition.secretive– secretive05/26/2019 22:10:40Commented May 26, 2019 at 22:10
For example like that:
list = ['π', 'π', 'π€£', 'π', 'π', 'π
', 'π', 'π', 'π', 'π', 'π', 'π', 'π', 'π']
text = set() #~ Set contains only unique elements
while len(text) < 3:
text.add(random.choice(list))
This should after while, always hold 3 elements, but it can take longer if pseudorandom will 'randomly' pick 999 times that same emoji, almost impossible, but there is a chance.
Another way, more constant in time is shuffle:
list = ['π', 'π', 'π€£', 'π', 'π', 'π
', 'π', 'π', 'π', 'π', 'π', 'π', 'π', 'π']
random.shuffle(list)
text = list[:3]
But that without makeing copy, always shuffle your first list
variable.
Offtopic: list
is key word in python interpreter, pls don't use variable named like function list()
in your code. I think it provokes 'minus' votes on you.
@EDIT:
At first, change variable name list
for array
.
Then, it should work:
@client.event
async def on_ready():
txtchan = client.get_channel(int(chan))
while not client.is_closed():
message = ''.join(random.sample(array, 3))
await txtchan.send(message)
client.run(token, bot=False)
If it won't work, and some error occured, send it to us. Without that, most of people there, can't easy and fast test it and tell you what wasn't correct.
And i hope it isn't your all what code contains, becouse it isn't enough to correctly login bot into discord ;)
-
the shuffle is work but for repetition its keep sending same emojiLimited Brain Cells– Limited Brain Cells05/26/2019 21:36:41Commented May 26, 2019 at 21:36
-
Yea, becouse u should shuffle before useing it next time, for example use it as function:
>>> def foo(arr): ... random.shuffle(arr) ... return arr[:3] ... >>> a [3, 6, 0, 5, 4, 9, 1, 2, 7, 8] >>> foo(a) [2, 4, 9] >>> a [2, 4, 9, 7, 5, 6, 8, 3, 0, 1] >>> foo(a) [8, 2, 3]
But you still will always got shuffled 'a', when you don't use copy.Guaz– Guaz05/26/2019 21:41:12Commented May 26, 2019 at 21:41 -
can you check my question, i got some script from friend but it cant worked maybe u can see itLimited Brain Cells– Limited Brain Cells05/26/2019 21:47:17Commented May 26, 2019 at 21:47
-
I already editted, maybe it should work, but read it, with commentaries, not only copy/paste, becouse u occure an errorGuaz– Guaz05/26/2019 21:56:04Commented May 26, 2019 at 21:56
-
Its working!, thanks alot for all the help Guaz, Hope u still healthy to helping peopleLimited Brain Cells– Limited Brain Cells05/26/2019 22:30:46Commented May 26, 2019 at 22:30
You can use random.choices()
:
import random
l = [*range(100)]
choices = random.choices(l, k=3)
print(choices)
Upd.
Sorry, I've missed that fact, that you need to pick unique values. For that you can use random.sample()
:
choices = random.sample(l, 3)
You can make a generator that yields from a random.sample
in a loop. Every time it's used up all of the sample the loop will continue and make a new sample. Then use itertools.slice
to grab however many you want for as long as you want.
from itertools import islice
import random
def ran(l):
while True:
yield from random.sample(l, len(l))
l = ['π', 'π', 'π€£', 'π', 'π', 'π
', 'π', 'π', 'π', 'π', 'π', 'π', 'π', 'π']
randIter = ran(l)
for i in range(20):
print(list(islice(randIter, 7))) # pull off 7 at a time to make it easy to verify:
Result
['π€£', 'π', 'π', 'π', 'π', 'π', 'π']
['π', 'π
', 'π', 'π', 'π', 'π', 'π']
['π', 'π', 'π', 'π', 'π', 'π', 'π']
['π
', 'π', 'π', 'π', 'π€£', 'π', 'π']
['π', 'π', 'π
', 'π', 'π', 'π€£', 'π']
['π', 'π', 'π', 'π', 'π', 'π', 'π']
['π', 'π', 'π', 'π', 'π', 'π', 'π
']
['π', 'π€£', 'π', 'π', 'π', 'π', 'π']
['π', 'π', 'π', 'π', 'π', 'π€£', 'π']
['π', 'π', 'π', 'π', 'π', 'π
', 'π']
['π', 'π', 'π', 'π', 'π
', 'π', 'π']
['π', 'π€£', 'π', 'π', 'π', 'π', 'π']
['π
', 'π', 'π', 'π', 'π', 'π', 'π']
['π', 'π', 'π', 'π€£', 'π', 'π', 'π']
['π', 'π', 'π', 'π', 'π', 'π', 'π']
['π', 'π', 'π', 'π', 'π
', 'π€£', 'π']
['π', 'π', 'π€£', 'π', 'π', 'π', 'π']
['π', 'π', 'π', 'π', 'π', 'π
', 'π']
['π
', 'π', 'π', 'π', 'π€£', 'π', 'π']
['π', 'π', 'π', 'π', 'π', 'π', 'π']
This uses all 14 in random order before repeating and each repeat gets a new order.
random.choice()
it gives a single result and is fixed. You would need to call the function additional times to get new random results. EDIT: your edit corrected this