Skip to content

Instantly share code, notes, and snippets.

@rkrzewski
Created December 11, 2013 17:46
Show Gist options
  • Save rkrzewski/7915056 to your computer and use it in GitHub Desktop.
Save rkrzewski/7915056 to your computer and use it in GitHub Desktop.
import scala.util.Random
object S99_P23 {
def removeAt[T](n: Int, ts: Seq[T]) = (ts.take(n) ++ ts.drop(n + 1), ts(n))
def randomSelect[T](count: Int, ts: Seq[T]): Seq[T] = {
val r = new Random
def go(count: Int, ts: Seq[T], acc: Seq[T]): Seq[T] = {
if (count > 0) {
val (next, elem) = removeAt(r.nextInt(ts.length), ts)
go(count - 1, next, elem +: acc)
}
else acc
}
go(count, ts, Seq.empty[T])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment