-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)
  • sorry if my question look ridicolous to you – Limited Brain Cells May 26 '19 at 21:26
  • Here is a great answer to the more generic question: https://stackoverflow.com/a/45969227/769486 – zwirbeltier May 26 '19 at 21:32
  • See https://stackoverflow.com/questions/2587387/python-picking-an-element-without-replacement – jonrsharpe May 26 '19 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 – Cireo May 26 '19 at 21:49

4 Answers4

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
  • 43,494
  • 9
  • 68
  • 117
secretive
  • 2,032
  • 7
  • 16
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 ;)

Guaz
  • 193
  • 1
  • 1
  • 12
  • the shuffle is work but for repetition its keep sending same emoji – Limited Brain Cells May 26 '19 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 May 26 '19 at 21:41
  • can you check my question, i got some script from friend but it cant worked maybe u can see it – Limited Brain Cells May 26 '19 at 21:47
  • I already editted, maybe it should work, but read it, with commentaries, not only copy/paste, becouse u occure an error – Guaz May 26 '19 at 21:56
  • Its working!, thanks alot for all the help Guaz, Hope u still healthy to helping people – Limited Brain Cells May 26 '19 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)
Olvin Roght
  • 7,677
  • 2
  • 16
  • 35
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.

Mark
  • 90,562
  • 7
  • 108
  • 148