summaryrefslogtreecommitdiff
path: root/src/library
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2010-04-13 18:24:27 +0000
committerPaul Phillips <paulp@improving.org>2010-04-13 18:24:27 +0000
commit27288e3ffe2224bb206e79204542c26a2779223b (patch)
tree399a6f78852660a4383db4f5018060eaf8c94329 /src/library
parent41d9ea14526b0cf64a5d2146616fd6acf3472e87 (diff)
downloadscala-27288e3ffe2224bb206e79204542c26a2779223b.tar.gz
scala-27288e3ffe2224bb206e79204542c26a2779223b.tar.bz2
scala-27288e3ffe2224bb206e79204542c26a2779223b.zip
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.
Diffstat (limited to 'src/library')
-rw-r--r--src/library/scala/util/Random.scala23
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