The probability approach is interesting too though.
If you use that method, you don't need to create extra variables, except maybe a single constant to define the probability. My method would require a timekeeper variable and up to two constants if you want to store the min and max times somewhere obvious so they can be easily changed.
That's a big upside, not needing that timer variable. Not only does it make the code cleaner, but you also will never need to initialize it. Or reset it. In my game, if an alien is offscreen and it has to fire, I have to store a new next fire time, or else it will fire immediately when it comes back on the screen. And, if I don't want enemies to fire immediately, I have to calculate the first fire time when they spawn. And that means I have to have the calculation code in two places.
There is a downside to your method though. It's not instantly obvious how often an event will occur. And since the event occurring is truly random, it is possible it will NEVER occur. Also, it is possible an event could occur twice within mere milliseconds.
With my method on the other hand, you have precise control over the minimum time between events, and the maximum time between events.
So while your method is the more beautiful one, mine is more practical. :-)
If you use that method, you don't need to create extra variables, except maybe a single constant to define the probability. My method would require a timekeeper variable and up to two constants if you want to store the min and max times somewhere obvious so they can be easily changed.
That's a big upside, not needing that timer variable. Not only does it make the code cleaner, but you also will never need to initialize it. Or reset it. In my game, if an alien is offscreen and it has to fire, I have to store a new next fire time, or else it will fire immediately when it comes back on the screen. And, if I don't want enemies to fire immediately, I have to calculate the first fire time when they spawn. And that means I have to have the calculation code in two places.
There is a downside to your method though. It's not instantly obvious how often an event will occur. And since the event occurring is truly random, it is possible it will NEVER occur. Also, it is possible an event could occur twice within mere milliseconds.
With my method on the other hand, you have precise control over the minimum time between events, and the maximum time between events.
So while your method is the more beautiful one, mine is more practical. :-)