Skip to main content

Insecure generation of random numbers


Secure generation of random numbers


  • Usage of Scala for building scalable and functional applications
  • Usage of scala.util.Random for generating random numbers in Scala


Non compliant code

import scala.util.Random

object VulnerableRandomGenerator {
def generateRandomNumber(): Int = {
val r = new Random()
r.nextInt(100) // generate a random number between 0 and 100

The above Scala code is a simple random number generator that uses the scala.util.Random class to generate a random integer.

The generateRandomNumber() function creates a new instance of Random and then generates a random integer between 0 and 100.

This code is vulnerable due to the insecure generation of random numbers. The Random class in Scala uses a linear congruential generator (LCG) under the hood, which is known to be insecure for cryptographic uses. This is because LCGs have short periods and correlations between successive values, which makes the random number sequence easy to predict after observing a few outputs.

Furthermore, the range of generated numbers is very small (only between 0 and 100), which further reduces the entropy of the generated numbers. This makes it even easier for an attacker to guess the next number in the sequence.

In summary, this code is vulnerable to attacks where an attacker can predict the next random number in the sequence, which could lead to serious security issues depending on the context in which these random numbers are used.


  • Replace the usage of scala.util.Random with a cryptographically secure random number generator.
  • Use a secure random number generator library, such as, to generate random numbers.
  • Ensure that the random number generator is properly seeded with sufficient entropy.
  • Generate random numbers from a larger range to increase the unpredictability.
  • Consider using a library or framework that provides secure random number generation functions.

Compliant code


object SecureRandomGenerator {
private val secureRandom = new SecureRandom()

def generateRandomNumber(): Int = {
secureRandom.nextInt(100) // generate a random number between 0 and 100

The above code fixes the vulnerability by replacing the insecure scala.util.Random with, which is a cryptographically strong random number generator (RNG).

The SecureRandom class provides a cryptographically strong random number generator (RNG). A cryptographically strong random number minimally complies with the statistical random number generator tests specified in FIPS 140-2, Security Requirements for Cryptographic Modules, section 4.9.1.

In the fixed code, we create a single instance of SecureRandom which is then used to generate random numbers. This is more secure and efficient than creating a new Random instance every time a random number is needed, as was done in the vulnerable code.

The nextInt(100) method is used to generate a random number between 0 and 100. This is similar to the vulnerable code, but now the random number is generated using a secure RNG.

This fix ensures that the random numbers generated are unpredictable and resistant to probabilistic attacks, addressing the identified vulnerability.