diff --git a/src/main/java/wtf/beatrice/hidekobot/Cache.java b/src/main/java/wtf/beatrice/hidekobot/Cache.java index e41476b..97016b0 100644 --- a/src/main/java/wtf/beatrice/hidekobot/Cache.java +++ b/src/main/java/wtf/beatrice/hidekobot/Cache.java @@ -14,6 +14,7 @@ import wtf.beatrice.hidekobot.util.Logger; import java.awt.*; import java.lang.reflect.Field; import java.time.LocalDateTime; +import java.util.Random; public class Cache { @@ -23,6 +24,10 @@ public class Cache private static final String botPrefix = "hideko"; private static final Logger logger = new Logger(Cache.class); + // the Random instance that we should always use when looking for an RNG based thing. + // the seed is updated periodically. + private static final Random randomInstance = new Random(); + private static PropertiesSource propertiesSource = null; private static ConfigurationSource configurationSource = null; private static DatabaseSource databaseSource = null; @@ -60,6 +65,14 @@ public class Cache */ public static int[] getSupportedAvatarResolutions() { return supportedAvatarResolutions; } + public static Random getRandom() { + return randomInstance; + } + + public static void setRandomSeed(long seed) { + randomInstance.setSeed(seed); + } + /** * Checks if the bot has been started with the verbose argument. * diff --git a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java index a4ad60d..527abed 100644 --- a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java +++ b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java @@ -18,6 +18,7 @@ import wtf.beatrice.hidekobot.listeners.SlashCommandCompletionListener; import wtf.beatrice.hidekobot.listeners.SlashCommandListener; import wtf.beatrice.hidekobot.runnables.ExpiredMessageTask; import wtf.beatrice.hidekobot.runnables.HeartBeatTask; +import wtf.beatrice.hidekobot.runnables.RandomSeedTask; import wtf.beatrice.hidekobot.runnables.StatusUpdateTask; import wtf.beatrice.hidekobot.util.CommandUtil; import wtf.beatrice.hidekobot.util.Logger; @@ -184,6 +185,8 @@ public class HidekoBot scheduler.scheduleAtFixedRate(heartBeatTask, 10, 30, TimeUnit.SECONDS); //every 30 seconds StatusUpdateTask statusUpdateTask = new StatusUpdateTask(); scheduler.scheduleAtFixedRate(statusUpdateTask, 0, 60 * 5, TimeUnit.SECONDS); // every 5 minutes + RandomSeedTask randomSeedTask = new RandomSeedTask(); + scheduler.scheduleAtFixedRate(randomSeedTask, 0, 60, TimeUnit.SECONDS); // every minute // register shutdown interrupt signal listener for proper shutdown. Signal.handle(new Signal("INT"), signal -> shutdown()); diff --git a/src/main/java/wtf/beatrice/hidekobot/runnables/RandomSeedTask.java b/src/main/java/wtf/beatrice/hidekobot/runnables/RandomSeedTask.java new file mode 100644 index 0000000..f4623a0 --- /dev/null +++ b/src/main/java/wtf/beatrice/hidekobot/runnables/RandomSeedTask.java @@ -0,0 +1,12 @@ +package wtf.beatrice.hidekobot.runnables; + +import wtf.beatrice.hidekobot.Cache; + +public class RandomSeedTask implements Runnable +{ + + @Override + public void run() { + Cache.setRandomSeed(System.currentTimeMillis()); + } +} diff --git a/src/main/java/wtf/beatrice/hidekobot/util/RandomUtil.java b/src/main/java/wtf/beatrice/hidekobot/util/RandomUtil.java index f9dec08..e3a8779 100644 --- a/src/main/java/wtf/beatrice/hidekobot/util/RandomUtil.java +++ b/src/main/java/wtf/beatrice/hidekobot/util/RandomUtil.java @@ -1,12 +1,11 @@ package wtf.beatrice.hidekobot.util; +import wtf.beatrice.hidekobot.Cache; + import java.util.Random; public class RandomUtil { - private static final Random random = new Random(); - - /** * Returns a random integer picked in a range. * @@ -29,7 +28,7 @@ public class RandomUtil int difference = (max - min) + 1; // find a number between 0 and our range (eg. 5 -> 8 = 0 -> 3) - int randomTemp = random.nextInt(difference); + int randomTemp = Cache.getRandom().nextInt(difference); // add the minimum value, so we are sure to be in the original range (0->5, 1->6, 2->7, 3->8) return randomTemp + min;