Scala: Create a random number between a min and max value (a range)

As a quick note, here’s a Scala function to generate a random integer value that will be in between the input min and max values:

import scala.util.Random

def randomIntBetween(min: Int, max: Int): Int =
    val random = Random()
    random.nextInt((max - min) + 1) + min

The values are inclusive, so if you supply 4 as the min and 8 as the max, the output will always be in the range 4, 5, 6, 7, and 8.

So if you need a Scala function to generate random numbers in between a range of numbers, I hope this is helpful.