From 27288e3ffe2224bb206e79204542c26a2779223b Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Tue, 13 Apr 2010 18:24:27 +0000 Subject: A safety improvement for random path generation... A safety improvement for random path generation, and a small change to Random for a more general interface. Review by community. --- src/library/scala/util/Random.scala | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'src/library') 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 -- cgit v1.2.3