Created
July 9, 2010 13:04
-
-
Save oluies/469431 to your computer and use it in GitHub Desktop.
from sbde http://technically.us/git?p=spde.git
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
trait Randoms { | |
import scala.util.Random | |
private val rand = new Random | |
implicit def seq2RichRandom[K](seq: Seq[K]) = new RichRandom(seq) | |
protected class RichRandom[K](seq: Seq[K]) { | |
def random: K = seq((rand.nextFloat * seq.length).toInt) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
scala> object MyImplicits extends Randoms
defined module MyImplicits
scala> import MyImplicits._
import MyImplicits._
scala> val l = List(1,2,3,4,5)
l: List[Int] = List(1, 2, 3, 4, 5)
scala> l.random
res0: Int = 4