Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Scaladoc and unit tests for FastSimpleRandom

  • Loading branch information...
commit 6cd564fddba185976a56aca15139af6068bce707 1 parent 350902e
@nbronson nbronson authored
View
20 src/main/scala/scala/concurrent/stm/skel/FastSimpleRandom.scala
@@ -33,6 +33,9 @@ private[stm] object FastSimpleRandom {
private val states = Array.tabulate(mask + 1)({ _ * 0x123456789abcdefL })
+ /** Returns a random value chosen from a uniform distribution of all valid
+ * `Int`s.
+ */
def nextInt(): Int = {
val id = (Thread.currentThread.getId.asInstanceOf[Int] * 13) & mask
@@ -42,8 +45,13 @@ private[stm] object FastSimpleRandom {
extract(next)
}
+ /** Returns a random value chosen from a uniform distribution of the
+ * non-negative integers less than `n`, or throws `IllegalArgumentException`
+ * if `n` is negative or zero.
+ */
def nextInt(n: Int): Int = {
- require(n > 0)
+ if (n <= 0)
+ throw new IllegalArgumentException
var x = -1
while (x == -1) x = tryClamp(nextInt(), n)
@@ -88,13 +96,21 @@ private[stm] final class FastSimpleRandom private (private var _state: Long, dum
override def clone = new FastSimpleRandom(_state, false)
+ /** Returns a random value chosen from a uniform distribution of all valid
+ * `Int`s.
+ */
def nextInt(): Int = {
_state = step(_state)
extract(_state)
}
+ /** Returns a random value chosen from a uniform distribution of the
+ * non-negative integers less than `n`, or throws `IllegalArgumentException`
+ * if `n` is negative or zero.
+ */
def nextInt(n: Int): Int = {
- require(n > 0)
+ if (n <= 0)
+ throw new IllegalArgumentException
var x = -1
while (x == -1) x = tryClamp(nextInt(), n)
View
65 src/test/scala/scala/concurrent/stm/skel/FastSimpleRandomSuite.scala
@@ -0,0 +1,65 @@
+/* scala-stm - (c) 2009-2011, Stanford University, PPL */
+
+package scala.concurrent.stm
+package skel
+
+import org.scalatest.FunSuite
+
+class FastSimpleRandomSuite extends FunSuite {
+
+ test("nextInt") {
+ val f = new FastSimpleRandom
+ val rand = new scala.util.Random
+ var s = 0
+ for (i <- 0 until 100000) {
+ s |= FastSimpleRandom.nextInt
+ s |= f.nextInt
+ }
+ assert(s != 0)
+ }
+
+ test("nextInt(n) in range") {
+ val f = new FastSimpleRandom
+ val rand = new scala.util.Random
+ for (i <- 0 until 100000) {
+ val n = rand.nextInt(Int.MaxValue - 1) + 1
+ val gr = FastSimpleRandom.nextInt(n)
+ assert(gr >= 0 && gr < n)
+ val lr = f.nextInt(n)
+ assert(lr >= 0 && lr < n)
+ }
+ }
+
+ test("clone") {
+ val f1 = new FastSimpleRandom
+ for (i <- 0 until 1000)
+ f1.nextInt
+ val f2 = f1.clone
+ for (i <- 0 until 1000)
+ assert(f1.nextInt(9999) === f2.nextInt(9999))
+ }
+
+ test("seeded") {
+ val f1 = new FastSimpleRandom(100)
+ val f2 = new FastSimpleRandom(100)
+ for (i <- 0 until 1000)
+ assert(f1.nextInt === f2.nextInt)
+ }
+
+ test("global FastSimpleRandom distribution") {
+ val buckets = new Array[Int](100)
+ for (i <- 0 until 100000)
+ buckets(FastSimpleRandom.nextInt(buckets.length)) += 1
+ for (b <- buckets)
+ assert(b > 0)
+ }
+
+ test("local FastSimpleRandom distribution") {
+ val f = new FastSimpleRandom
+ val buckets = new Array[Int](100)
+ for (i <- 0 until 100000)
+ buckets(f.nextInt(buckets.length)) += 1
+ for (b <- buckets)
+ assert(b > 0)
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.