I have made a game with pygame, where I used random module to choose a random variables from a list, but somehow the same variable is getting chosen.
Do someone knows anything about this,?
You probably haven’t seeded your RNG. Do random.seed() when you first launch the game.
The variable i m saying here is the selection of the mafia in my game and also the events.
https://danger-drago.itch.io/Guess-the-mafia
Code :
Suspects= ['Alex','Sam','Emily','Ben','David','Tom','Arlond','Erica','John','Selly']
mafia= random.choice(Suspects)
# This is the part of my code which is not working as it should!
Well if you want to review the whole code, i have posted it over my game, (the above link.)
Your problem isn't really with Python, but with pygbag (something you don't mention).
Look, Python always changes the random seed by default, and the code above is more than enough to get a different result every game. However, pygbag starts games in reproducible mode by default. Therefore, random.choice always selects the same element in each game.
Simply run this line before starting to use random:
random.seed(time.time())