Last active
October 11, 2015 03:47
-
-
Save mepcotterell/3797803 to your computer and use it in GitHub Desktop.
Shuffled (Uniform Random) Range in Scala
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
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) } |
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
import ShuffledRangeImplicits._ | |
object ExampleApp extends App { | |
(0 until 100 shuffled) foreach { i => println(i) } | |
} |
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
import util.Random.shuffle | |
shuffle(1 to 20) foreach println |
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
class ShuffledRange(r: Range) { | |
def shuffled = new scala.util.Random().shuffle(r toIterable) | |
} |
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
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