I’m trying to make a game purely out of python, and I’m trying to make a system that runs a def randomly, does anyone know how? (Not making a system where it picks a random def, instead, it runs a def randomly depending on time) Any library is fine too.
Edit 1: The def I was referring to was a function where you get a random chest out of luck (which the “luck” is what I’m trying to make). My expected output is the def to run. In this case, me getting the first print in the def “You found a chest”.
Edit 2: What I meant by “making a system where it picks a random def” was a function where it randomly picks a def. And “it runs a def randomly depending on time” is a function where it runs the def by “luck”
Advertisement
Answer
In case you wanted to just make a function that will have a certain chance every X time interval to trigger, it would look something like this:
import time import random def Foo(): ... interval = 10 # Seconds chance = 50 # Percent chance while True: time.sleep(interval) if random.randint(1, 101) > chance: Foo()
Note that this only gives you a 50% chance that Foo
will trigger every 10 seconds, which is not truly random. To increase the randomness, you may want to make the chance of this event happening random, as well as the time interval.