Skip to content

add Random.nextLong(Long) #639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import scala.collection.{
mutable => m
}
import scala.runtime.{Tuple2Zipped, Tuple3Zipped}
import scala.util.Random
import scala.{collection => c}

/** The collection compatibility API */
Expand Down Expand Up @@ -351,6 +352,9 @@ private[compat] trait PackageShared {

implicit def toOptionCompanionExtension(fact: Option.type): OptionCompanionExtensionMethods =
new OptionCompanionExtensionMethods(fact)

implicit def toRandomExtensions(self: Random): RandomExtensions =
new RandomExtensions(self)
}

class ImmutableSortedMapExtensions(private val fact: i.SortedMap.type) extends AnyVal {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Scala (https://www.scala-lang.org)
*
* Copyright EPFL and Lightbend, Inc.
*
* Licensed under Apache License 2.0
* (http://www.apache.org/licenses/LICENSE-2.0).
*
* See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*/

package scala.collection.compat

import scala.util.Random

final class RandomExtensions(private val self: Random) extends AnyVal {
def nextLong(n: Long): Long = {
require(n > 0, "n must be positive")

var offset = 0L
var _n = n

while (_n >= Integer.MAX_VALUE) {
val bits = self.nextInt(2)
val halfn = _n >>> 1
val nextn =
if ((bits & 2) == 0) halfn
else _n - halfn
if ((bits & 1) == 0)
offset += _n - nextn
_n = nextn
}
offset + self.nextInt(_n.toInt)
}
}
34 changes: 34 additions & 0 deletions compat/src/test/scala/test/scala/util/RandomTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Scala (https://www.scala-lang.org)
*
* Copyright EPFL and Lightbend, Inc.
*
* Licensed under Apache License 2.0
* (http://www.apache.org/licenses/LICENSE-2.0).
*
* See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*/

package test.scala.util

import org.junit.Assert._
import org.junit.Test
import scala.collection.compat._
import scala.util.Random
import test.scala.collection.AssertThrown

class RandomTest extends AssertThrown {
@Test
def nextLong(): Unit = {
val rand = new Random(12345)

assertEquals(4896762128577075113L, rand.nextLong(Long.MaxValue))
assertEquals(2005076556L, rand.nextLong(Int.MaxValue))
assertEquals(0L, rand.nextLong(1L))

assertThrows[IllegalArgumentException](rand.nextLong(0L))
assertThrows[IllegalArgumentException](rand.nextLong(-2L))
assertThrows[IllegalArgumentException](rand.nextLong(Long.MinValue))
}
}