Created
April 23, 2012 10:31
-
-
Save notyy/2470138 to your computer and use it in GitHub Desktop.
random
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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