Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting non-duplicate random numbers in discord.py

I’ve been working on a command that returns a random string using a dictionary and the random library:

import random
randnum = random.randint(0,5)

words = {1:"random text" 2:"random text2" 3:"random text3" 4:"random text4" 5:"randomtext5"}

def getrandom():
    randomtext = words[randnum]
    return randomtext

But whenever I use the command it returns the same text since I’m getting the same number over and over again, I also tried using the random.sample method, but I’m getting the same results, I don’t have much experience with this library,
Is there any way for me to get a random non-duplicate number? any help would be appreciated!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Generate the random number inside the getrandom() method.

import random

words = {1:"random text" 2:"random text2" 3:"random text3" 4:"random text4" 5:"randomtext5"}

def getrandom():
    randnum = random.randint(0,5)
    randomtext = words[randnum]
    return randomtext
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading