Increase randomness by updating the random's seed every minute

This commit is contained in:
Bea 2022-12-20 22:15:52 +01:00
parent 264a94fe0d
commit ba64c02049
4 changed files with 31 additions and 4 deletions

View File

@ -14,6 +14,7 @@ import wtf.beatrice.hidekobot.util.Logger;
import java.awt.*; import java.awt.*;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Random;
public class Cache public class Cache
{ {
@ -23,6 +24,10 @@ public class Cache
private static final String botPrefix = "hideko"; private static final String botPrefix = "hideko";
private static final Logger logger = new Logger(Cache.class); 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 PropertiesSource propertiesSource = null;
private static ConfigurationSource configurationSource = null; private static ConfigurationSource configurationSource = null;
private static DatabaseSource databaseSource = null; private static DatabaseSource databaseSource = null;
@ -60,6 +65,14 @@ public class Cache
*/ */
public static int[] getSupportedAvatarResolutions() { return supportedAvatarResolutions; } 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. * Checks if the bot has been started with the verbose argument.
* *

View File

@ -18,6 +18,7 @@ import wtf.beatrice.hidekobot.listeners.SlashCommandCompletionListener;
import wtf.beatrice.hidekobot.listeners.SlashCommandListener; import wtf.beatrice.hidekobot.listeners.SlashCommandListener;
import wtf.beatrice.hidekobot.runnables.ExpiredMessageTask; import wtf.beatrice.hidekobot.runnables.ExpiredMessageTask;
import wtf.beatrice.hidekobot.runnables.HeartBeatTask; import wtf.beatrice.hidekobot.runnables.HeartBeatTask;
import wtf.beatrice.hidekobot.runnables.RandomSeedTask;
import wtf.beatrice.hidekobot.runnables.StatusUpdateTask; import wtf.beatrice.hidekobot.runnables.StatusUpdateTask;
import wtf.beatrice.hidekobot.util.CommandUtil; import wtf.beatrice.hidekobot.util.CommandUtil;
import wtf.beatrice.hidekobot.util.Logger; import wtf.beatrice.hidekobot.util.Logger;
@ -184,6 +185,8 @@ public class HidekoBot
scheduler.scheduleAtFixedRate(heartBeatTask, 10, 30, TimeUnit.SECONDS); //every 30 seconds scheduler.scheduleAtFixedRate(heartBeatTask, 10, 30, TimeUnit.SECONDS); //every 30 seconds
StatusUpdateTask statusUpdateTask = new StatusUpdateTask(); StatusUpdateTask statusUpdateTask = new StatusUpdateTask();
scheduler.scheduleAtFixedRate(statusUpdateTask, 0, 60 * 5, TimeUnit.SECONDS); // every 5 minutes 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. // register shutdown interrupt signal listener for proper shutdown.
Signal.handle(new Signal("INT"), signal -> shutdown()); Signal.handle(new Signal("INT"), signal -> shutdown());

View File

@ -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());
}
}

View File

@ -1,12 +1,11 @@
package wtf.beatrice.hidekobot.util; package wtf.beatrice.hidekobot.util;
import wtf.beatrice.hidekobot.Cache;
import java.util.Random; import java.util.Random;
public class RandomUtil public class RandomUtil
{ {
private static final Random random = new Random();
/** /**
* Returns a random integer picked in a range. * Returns a random integer picked in a range.
* *
@ -29,7 +28,7 @@ public class RandomUtil
int difference = (max - min) + 1; int difference = (max - min) + 1;
// find a number between 0 and our range (eg. 5 -> 8 = 0 -> 3) // 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) // 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; return randomTemp + min;