Skip to content

Instantly share code, notes, and snippets.

@notyy
Created April 23, 2012 10:31
Show Gist options
  • Save notyy/2470138 to your computer and use it in GitHub Desktop.
Save notyy/2470138 to your computer and use it in GitHub Desktop.
random
def shuffle[T](t: Int, l: List[T]): List[T] = (t,l) match {
case (0, _) => Nil
case (x, Nil) => Nil
case (x, ls) => {
import scala.util.Random
val r = ls(Random.nextInt(ls.length))
r :: shuffle(x-1, ls.filterNot(item => item == r))
}
}
===============================================
scala> val list = ('a' to 'z').toList
list: List[Char] = List(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z)
scala> shuffle(10, list)
res0: List[Char] = List(a, m, b, k, w, f, t, s, c, q)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment