I want to create a set of randomly selected indices from an input collection observations
:
case class Observation(id: Long, metric1: Double)
val observations: Seq[Observation]
val NumSamples = 100
val indices = // A set of randomly selected indices of the observations
// WITHOUT replacement
The complication is that to avoid replacement of the existing indices when selecting new ones (via myRandom.nextInt(observations.length
) we need to have access to the prior ones - which is afaik not possible during the initial generation of a sequence.
An outline of what I'm looking for is shown here
Most preferred (but I doubt it can be done..)
val sampledIndices: Seq[Int] = for (randInd <- 0 until NSamples) yield {
// some random non-repeated index in [0..length(observations)]
}
But following is a second choice:
val randomIndices = mutable.ArrayBuffer[Int]()
for (randInd <- 0 until NSamples) {
randomIndices ++= // some random non-repeated index in
}
What to avoid: multiple var
s .. which is what I am running into so far.