Skip to content

Instantly share code, notes, and snippets.

@mepcotterell
Last active October 11, 2015 03:47
Show Gist options
  • Save mepcotterell/3797803 to your computer and use it in GitHub Desktop.
Save mepcotterell/3797803 to your computer and use it in GitHub Desktop.
Shuffled (Uniform Random) Range in Scala
import scala.util.Random
// print unshuffled range
(1 to 10) foreach { i => println(i) }
// print shuffled range
val r = new Random()
r.shuffle(1 to 10 toIterable) foreach { i => println(i) }
import ShuffledRangeImplicits._
object ExampleApp extends App {
(0 until 100 shuffled) foreach { i => println(i) }
}
import util.Random.shuffle
shuffle(1 to 20) foreach println
class ShuffledRange(r: Range) {
def shuffled = new scala.util.Random().shuffle(r toIterable)
}
object ShuffledRangeImplicits {
implicit def mkRandomRange (r: Range) = new ShuffledRange(r)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment