how to randomize period in Timer?

422 Views Asked by At

Whenever I run this code:

timer.scheduleAtFixedRate(task1, 0, (long) (Math.random() * 3000) + 500);

it executes the timer at the specified period, but it sets the random time initially instead of randomizing the period each time this runs. Is there perhaps a different way to go about doing this?

If I wasn't clear enough, my code runs task1 at a random number that I set the period for, but once it sets that random number, that random number is consistently used each time task1 runs. I'm trying to get it so that the period is a random number each time task1 runs, not having a consistent value.

1

There are 1 best solutions below

0
On

You have to reschedule after each run of task1 using regular timer.schedule(task1, 0, (long) (Math.random() * 3000) + 500); not scheduleAtFixedRate