-3

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)
asked May 26, 2019 at 21:22
4
  • sorry if my question look ridicolous to you Commented May 26, 2019 at 21:26
  • Here is a great answer to the more generic question: stackoverflow.com/a/45969227/769486 Commented May 26, 2019 at 21:32
  • See stackoverflow.com/questions/2587387/… Commented May 26, 2019 at 21:44
  • @LimitedBrainCells I think the main issue here may actually be a misunderstanding on how variables work. When you call 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 Commented May 26, 2019 at 21:49

4 Answers 4

2

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.

Carcigenicate
46.3k12 gold badges78 silver badges131 bronze badges
answered May 26, 2019 at 21:27
1
  • it does prevent repetition. Every time you run this code, the output will not contain repetition. Commented May 26, 2019 at 22:10
0

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

answered May 26, 2019 at 21:29
5
  • the shuffle is work but for repetition its keep sending same emoji Commented 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. Commented 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 it Commented 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 error Commented May 26, 2019 at 21:56
  • Its working!, thanks alot for all the help Guaz, Hope u still healthy to helping people Commented May 26, 2019 at 22:30
0

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)
answered May 26, 2019 at 21:41
0

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.

answered May 26, 2019 at 21:50

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.