For the solution, here's a Scala 3 function that generates a random sequence of integer values: import scala.util.Random def generateUniqueRandomNumbers( count: Int, minInclusive: Int, maxExclusive: Int ): Vector[Int] = require(maxExclusive - minInclusive + 1 >= count, "Range is too small to generate the desired number of unique random numbers") val random = Random() var numbers = Set.empty[Int] while numbers.size < count do numbers += random.nextInt((maxExclusive - minInclusive) + 1) + minInclusive numbers.toVector // call the function and use it like this val uniqueRandomNumbers = generateUniqueRandomNumbers(20, 1, 25) println(uniqueRandomNumbers)
The require part of this solution is extremely important. If you don't include it --- or something similar to it, like your own custom Exception --- you can create an infinite loop.
Collection
[
|
...
]