How can I do a random event in Java?

advertisements

So I'm making a game, and I want the user to be able to fight crimes. Only I don't want them to be able to fight crimes again for a period of time, a 10 second delay max. I know that I could use Math.Random() somehow, but I'm having trouble figuring out how to use it in this context.

To clarify, I want a label and two buttons saying yes or no to become visible after a random period of time, 20 seconds max. Can someone explain how this could be done, or if there is a better way to do this?

Thanks in advance.


Basicly you create a timestamp like this long timestamp = System.currentTimeMillis() after you have done the "fight". You instantly can disable the button now

Then you can create a Random object and call it´s next int method with the span you want to create for this 10-20 sec like this

Random r = new Random();
int span = r.nextInt(10);

Furthermore you just mathematicly add id up to milliseconds

span += 1000;

Then you would create a method that would be called in your gui update function, or a seperate thread, which would check the currentMillis against the timestamp of your last "fight".

if(System.currentTimeMillis() >= span + timestamp) {
    // activate
}