File tree Expand file tree Collapse file tree 3 files changed +74
-0
lines changed
main/scala-2.11_2.12/scala/collection/compat
test/scala/test/scala/util Expand file tree Collapse file tree 3 files changed +74
-0
lines changed Original file line number Diff line number Diff line change @@ -25,6 +25,7 @@ import scala.collection.{
2525 mutable => m
2626}
2727import scala .runtime .{Tuple2Zipped , Tuple3Zipped }
28+ import scala .util .Random
2829import scala .{collection => c }
2930
3031/** The collection compatibility API */
@@ -351,6 +352,9 @@ private[compat] trait PackageShared {
351352
352353 implicit def toOptionCompanionExtension (fact : Option .type ): OptionCompanionExtensionMethods =
353354 new OptionCompanionExtensionMethods (fact)
355+
356+ implicit def toRandomExtensions (self : Random ): RandomExtensions =
357+ new RandomExtensions (self)
354358}
355359
356360class ImmutableSortedMapExtensions (private val fact : i.SortedMap .type ) extends AnyVal {
Original file line number Diff line number Diff line change 1+ /*
2+ * Scala (https://www.scala-lang.org)
3+ *
4+ * Copyright EPFL and Lightbend, Inc.
5+ *
6+ * Licensed under Apache License 2.0
7+ * (http://www.apache.org/licenses/LICENSE-2.0).
8+ *
9+ * See the NOTICE file distributed with this work for
10+ * additional information regarding copyright ownership.
11+ */
12+
13+ package scala .collection .compat
14+
15+ import scala .util .Random
16+
17+ final class RandomExtensions (private val self : Random ) extends AnyVal {
18+ def nextLong (n : Long ): Long = {
19+ require(n > 0 , " n must be positive" )
20+
21+ var offset = 0L
22+ var _n = n
23+
24+ while (_n >= Integer .MAX_VALUE ) {
25+ val bits = self.nextInt(2 )
26+ val halfn = _n >>> 1
27+ val nextn =
28+ if ((bits & 2 ) == 0 ) halfn
29+ else _n - halfn
30+ if ((bits & 1 ) == 0 )
31+ offset += _n - nextn
32+ _n = nextn
33+ }
34+ offset + self.nextInt(_n.toInt)
35+ }
36+ }
Original file line number Diff line number Diff line change 1+ /*
2+ * Scala (https://www.scala-lang.org)
3+ *
4+ * Copyright EPFL and Lightbend, Inc.
5+ *
6+ * Licensed under Apache License 2.0
7+ * (http://www.apache.org/licenses/LICENSE-2.0).
8+ *
9+ * See the NOTICE file distributed with this work for
10+ * additional information regarding copyright ownership.
11+ */
12+
13+ package test .scala .util
14+
15+ import org .junit .Assert ._
16+ import org .junit .Test
17+ import scala .collection .compat ._
18+ import scala .util .Random
19+ import test .scala .collection .AssertThrown
20+
21+ class RandomTest extends AssertThrown {
22+ @ Test
23+ def nextLong (): Unit = {
24+ val rand = new Random (12345 )
25+
26+ assertEquals(4896762128577075113L , rand.nextLong(Long .MaxValue ))
27+ assertEquals(2005076556L , rand.nextLong(Int .MaxValue ))
28+ assertEquals(0L , rand.nextLong(1L ))
29+
30+ assertThrows[IllegalArgumentException ](rand.nextLong(0L ))
31+ assertThrows[IllegalArgumentException ](rand.nextLong(- 2L ))
32+ assertThrows[IllegalArgumentException ](rand.nextLong(Long .MinValue ))
33+ }
34+ }
You can’t perform that action at this time.
0 commit comments