Move random number generator to config

Signed-off-by: Rahul Rudragoudar <rr83019@gmail.com>
This commit is contained in:
Rahul Rudragoudar 2021-04-20 03:05:13 +05:30
parent 9e7efc1cf3
commit ef31ee8a57
No known key found for this signature in database
GPG Key ID: 0D2CE231A7287EBC
2 changed files with 8 additions and 8 deletions

View File

@ -23,15 +23,8 @@ object CaptchaProviders {
}
}
private val seed = Config.seed
private val random = new scala.util.Random(seed)
private val config = Config.captchaConfig
private def getNextRandomInt(max: Int): Int =
random.synchronized {
random.nextInt(max)
}
def getProviderById(id: String): ChallengeProvider = {
return providers(id)
}
@ -58,7 +51,7 @@ object CaptchaProviders {
def getProvider(param: Parameters): Option[ChallengeProvider] = {
val providerConfig = filterProviderByParam(param).toList
if (providerConfig.length > 0) {
val randomIndex = getNextRandomInt(providerConfig.length)
val randomIndex = Config.getNextRandomInt(providerConfig.length)
val providerIndex = providerConfig(randomIndex)._1
val selectedProvider = providers(providerIndex)
selectedProvider.configure(providerConfig(randomIndex)._2)

View File

@ -49,6 +49,13 @@ 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)
}
private def getDefaultConfig(): String = {
val defaultConfigMap =
(AttributesEnum.RANDOM_SEED.toString -> new ju.Random().nextInt()) ~