Skip to main content

On Sale: GamesAssetsToolsTabletopComics
Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Pygame project, random module problem

A topic by Danger_Drago created Aug 06, 2025 Views: 295 Replies: 8
Viewing posts 1 to 8

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,?

Moderator moved this topic to General Development
Moderator

Showing your code would help. And what do you mean, choosing a variable?

(1 edit) (+2)

You probably haven’t seeded your RNG. Do random.seed() when you first launch the game.

(+1)

As @notimetoplay says, it's best if you show some code, but one way to select a random item within a list is with "choice"

import random  
foo = ['a', 'b', 'c', 'd', 'e'] 
print(random.choice(foo))

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

Moderator

That should work. What exactly isn't going well?

(1 edit) (+1)

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())
(1 edit)

Thank you 👍, i will try it for sure!

Yea, it's working now! 

Thank you hechelion, for your advice.