Skip to content

Commit fbb4586

Browse files
committed
move XORShiftRandom to util.random and use it in BernoulliSampler
1 parent 987456b commit fbb4586

File tree

5 files changed

+11
-8
lines changed

5 files changed

+11
-8
lines changed

core/src/main/scala/org/apache/spark/util/Vector.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.spark.util
1919

2020
import scala.util.Random
21+
import org.apache.spark.util.random.XORShiftRandom
2122

2223
class Vector(val elements: Array[Double]) extends Serializable {
2324
def length = elements.length

core/src/main/scala/org/apache/spark/util/random/RandomSampler.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ trait RandomSampler[T, U] extends Pseudorandom with Cloneable with Serializable
4747
* @tparam T item type
4848
*/
4949
class BernoulliSampler[T](lb: Double, ub: Double, complement: Boolean = false)
50-
(implicit random: Random = new Random)
50+
(implicit random: Random = new XORShiftRandom)
5151
extends RandomSampler[T, T] {
5252

53-
def this(ratio: Double)(implicit random: Random = new Random) = this(0.0d, ratio)(random)
53+
def this(ratio: Double)(implicit random: Random = new XORShiftRandom)
54+
= this(0.0d, ratio)(random)
5455

5556
override def setSeed(seed: Long) = random.setSeed(seed)
5657

core/src/main/scala/org/apache/spark/util/XORShiftRandom.scala renamed to core/src/main/scala/org/apache/spark/util/random/XORShiftRandom.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
package org.apache.spark.util
18+
package org.apache.spark.util.random
1919

2020
import java.util.{Random => JavaRandom}
2121
import org.apache.spark.util.Utils.timeIt
@@ -46,6 +46,10 @@ private[spark] class XORShiftRandom(init: Long) extends JavaRandom(init) {
4646
seed = nextSeed
4747
(nextSeed & ((1L << bits) -1)).asInstanceOf[Int]
4848
}
49+
50+
override def setSeed(s: Long) {
51+
seed = s
52+
}
4953
}
5054

5155
/** Contains benchmark method and main method to run benchmark of the RNG */

core/src/test/scala/org/apache/spark/util/XORShiftRandomSuite.scala renamed to core/src/test/scala/org/apache/spark/util/random/XORShiftRandomSuite.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@
1515
* limitations under the License.
1616
*/
1717

18-
package org.apache.spark.util
18+
package org.apache.spark.util.random
1919

20-
import java.util.Random
21-
import org.scalatest.FlatSpec
2220
import org.scalatest.FunSuite
2321
import org.scalatest.matchers.ShouldMatchers
2422
import org.apache.spark.util.Utils.times

mllib/src/main/scala/org/apache/spark/mllib/clustering/KMeans.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ import org.apache.spark.SparkContext._
2626
import org.apache.spark.rdd.RDD
2727
import org.apache.spark.Logging
2828
import org.apache.spark.mllib.util.MLUtils
29-
import org.apache.spark.util.XORShiftRandom
30-
29+
import org.apache.spark.util.random.XORShiftRandom
3130

3231

3332
/**

0 commit comments

Comments
 (0)