Getting non-duplicate random numbers in discord.py

0

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!

discord discord.py python random
2021-11-23 16:10:58
2

0

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
2021-11-23 16:27:29
0

You need to get the random number in your function so it generates a new number each time it is called:

def getrandom():
    randnum = random.randint(0,5)
    randomtext = words[randnum]
    return randomtext
2021-11-23 16:13:28

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................