Skip to content
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

seedUniquifier ritual doesn't uniquify seeds #902

Open
bbjubjub2494 opened this issue Apr 29, 2020 · 0 comments
Open

seedUniquifier ritual doesn't uniquify seeds #902

bbjubjub2494 opened this issue Apr 29, 2020 · 0 comments

Comments

@bbjubjub2494
Copy link
Contributor

In random.rng.Utils.longFromSeed, a @volatile counter variable is used. The intent is presumably to guarantee that two RNG constructions initiated during the same nanosecond will result in two independent RNGs. The problem is that the @volatile construct isn't enough to secure a proper concurrent counter variable: it is very possible for two clients to receive the same value from the counter. Thus, the uniquifier isn't doing what it says on the label. This is showcased by the attached ammonite script. (Please run a few times if the result is ∅ at first)

This could be solved using different concurrency primitives, such as synchronized or monix.execution.atomic.

import $ivy.`org.typelevel::spire:0.17.0-M1`

import spire.random.rng.Utils.longFromTime

import concurrent.{Future, ExecutionContext, Await}
import concurrent.duration._

val nanoTime = 0L  // fake frozen timer

// collect 100 values from the uniquifier successively
def observeUniquifier()(implicit ec: ExecutionContext): Future[Vector[Long]] =
  Future { Vector.fill(100) { longFromTime(nanoTime) } }

@main def main(): Unit = {
  implicit val ec = ExecutionContext.global
  // collect two concurrent sequences from the uniquifier
  val (l1, l2) = Await.result(observeUniquifier() zip observeUniquifier(), Duration.Inf)
  // There should be no overlap, but there usually is
  println(l1.toSet & l2.toSet)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant