diff options
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/util/Random.scala | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/src/library/scala/util/Random.scala b/src/library/scala/util/Random.scala index ffa248d638..cd9d5eed0e 100644 --- a/src/library/scala/util/Random.scala +++ b/src/library/scala/util/Random.scala @@ -89,14 +89,12 @@ class Random(val self: java.util.Random) { List.fill(length)(safeChar()).mkString } - /** Returns a pseudorandomly generated String drawing upon - * only ASCII characters between 33 and 126. + /** Returns the next pseudorandom, uniformly distributed value + * from the ASCII range 33-126. */ - def nextASCIIString(length: Int) = { - val (min, max) = (33, 126) - def nextDigit = nextInt(max - min) + min - - new String(Array.fill(length)(nextDigit.toByte), "ASCII") + def nextPrintableChar(): Char = { + val (low, high) = (33, 126) + (self.nextInt(high - low) + low).toChar } def setSeed(seed: Long) { self.setSeed(seed) } @@ -111,6 +109,17 @@ object Random extends Random { import collection.mutable.ArrayBuffer import collection.generic.CanBuildFrom + /** Returns a Stream of pseudorandomly chosen alphanumeric characters, + * equally chosen from A-Z, a-z, and 0-9. + * + * @since 2.8 + */ + def alphanumeric: Stream[Char] = { + def isAlphaNum(c: Char) = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') + + Stream continually nextPrintableChar filter isAlphaNum + } + /** Returns a new collection of the same type in a randomly chosen order. * * @param coll the TraversableOnce to shuffle |