Move random number generator to HelperFunctions

Signed-off-by: Rahul Rudragoudar <rr83019@gmail.com>
This commit is contained in:
Rahul Rudragoudar 2021-04-24 19:36:48 +05:30
parent d62951fa51
commit 0d33f51f9e
No known key found for this signature in database
GPG Key ID: 0D2CE231A7287EBC
2 changed files with 15 additions and 9 deletions

View File

@ -1,9 +1,14 @@
package lc.misc;
import java.awt.*;
import java.util.Random;
public class HelperFunctions {
public static int seed;
private static Random random = new Random(seed);
public static void setRenderingHints(Graphics2D g2d) {
g2d.setRenderingHint(
RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
@ -25,13 +30,18 @@ public class HelperFunctions {
public static String randomString(final int n, final String characters) {
final StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < n; i++) {
int index = (int) (characters.length() * Math.random());
int index = randomNumber(characters.length());
stringBuilder.append(characters.charAt(index));
}
return stringBuilder.toString();
}
public static int randomNumber(int min, int max) {
return (int) (Math.random() * ((max - min) + 1)) + min;
synchronized public static int randomNumber(int min, int max) {
return (random.nextInt() * ((max - min) + 1)) + min;
}
synchronized public static int randomNumber(int bound) {
return random.nextInt(bound);
}
}

View File

@ -6,6 +6,7 @@ import org.json4s.jackson.JsonMethods.{parse, render, pretty}
import org.json4s.JsonDSL._
import java.io.{FileNotFoundException, File, PrintWriter}
import java.{util => ju}
import lc.misc.HelperFunctions
object Config {
@ -49,12 +50,7 @@ object Config {
val allowedMedia: Set[String] = captchaConfig.flatMap(_.allowedMedia).toSet
val allowedInputType: Set[String] = captchaConfig.flatMap(_.allowedInputType).toSet
private val random = new scala.util.Random(seed)
def getNextRandomInt(max: Int): Int =
random.synchronized {
random.nextInt(max)
}
HelperFunctions.seed = seed
private def getDefaultConfig(): String = {
val defaultConfigMap =