For arrays:
import scala.util.Random import scala.reflect.ClassTag def takeSample[T:ClassTag](a:Array[T],n:Int,seed:Long) = { val rnd = new Random(seed) Array.fill(n)(a(rnd.nextInt(a.size))) }
Create a random number generator ( rnd ) based on your seed. Then fill the array with random numbers from 0 to the size of your array.
The final step is to apply each random value to the index operator of your input array. Using it in a REPL might look like this:
scala> val myArray = Array(1,3,5,7,8,9,10) myArray: Array[Int] = Array(1, 3, 5, 7, 8, 9, 10) scala> takeSample(myArray,20,System.currentTimeMillis) res0: scala.collection.mutable.ArraySeq[Int] = ArraySeq(7, 8, 7, 3, 8, 3, 9, 1, 7, 10, 7, 10, 1, 1, 3, 1, 7, 1, 3, 7)
For lists, I would just convert the list to Array and use the same function. I doubt that you can still become much more efficient for lists.
It is important to note that the same function using lists will take O (n ^ 2) time, while first converting the list into arrays will take O (n) time