3

Simple novice problem, which I strangely haven't been able to conjure a solution for.

I'm making a simple dice roll simulator so I can compare nontransitive dice, as well as normal ones, however the issue is that if I make two dice with the same number of faces and values on said faces, both dice will roll the same value every time. (that is, each roll produces a different number, but both dice have the same value)

Here's my code:

class Die(values: Int*) {
  private val rand: util.Random = new util.Random(compat.Platform.currentTime)
  private val high = values.size + 1
  private val low  = values(0)
  def roll(): Int  = rand.nextInt(high - low) + low
  def this(vals: Range) = this(vals: _*)

  def rollAndCompareTo(that: Die): Symbol = {
    val a = this.roll()
    val b = that.roll()
    if(a > b) 'GT
    else if (a < b) 'LT
    else 'EQ
  }
}

object Program extends App {
  val d61 = new Die(1 to 6)
  val d62 = new Die(1 to 6)

  for(_ <- 1 to 100)
    println(d61 rollAndCompareTo d62)
}

100% of the time, the program will print nothing but 'EQ, because the two dice, despite being different instances created at different times always roll the same value.

I've also tried to add a delay, so that the seed difference is greater, but that doesn't help either.

What would I do to mend this?

7
  • Does currentTime actually change between the creation of those two instances of Die? Aug 13, 2014 at 20:04
  • 2
    Yeah you should be using a global instance of Random.
    – Andrew
    Aug 13, 2014 at 20:05
  • @RobertHarvey yes, it does change Aug 13, 2014 at 20:06
  • 1
    Cannot reproduce: ideone.com/IJULKD Aug 13, 2014 at 20:06
  • If you create several instances of random within 16ms or so, the time and thus the seed will be the same. (At least of compat.Platform.currentTime is the usual low resolution clock) Aug 13, 2014 at 20:07

4 Answers 4

3

Try leaving out the seed for your instance of Random, or use

new util.Random(System.currentTimeMillis)

The specificity of your seed is important if you're making many calls in a short amount of time.

5
  • 1
    Platform.currentTime already returns time in milliseconds resolution. Aug 13, 2014 at 20:09
  • It appears to be random when it does it. after I changed from Platform.currentTime to System.currentTimeMillis it worked fine for about 4 test runs before going back to mis-behaving Aug 13, 2014 at 20:10
  • 2
    @ElectricCoffee: You need to use a single instance of Random. Aug 13, 2014 at 20:11
  • @RobertHarvey so basically change from new util.Random() to util.Random to use its object? Aug 13, 2014 at 20:12
1

As said in other comments you can use a single rand on in the companion object then you don't have to worry about the resolution of the seed. Consider using a SecureRandom else using that to seed your single regular util.Random to avoid the overhead or possibly blocking behaviour of SecureRandom

object Die {
  // consider using java.security.SecureRandom or using that to seed a util.Random
  private[Die] val rand: util.Random = new util.Random(compat.Platform.currentTime)
}

class Die(values: Int*) {
  private val high = values.size + 1
  private val low = values(0)
  def roll(): Int = Die.rand.nextInt(high - low) + low
  def this(vals: Range) = this(vals: _*)

  def rollAndCompareTo(die: Die): Symbol = {
    val a = this.roll()
    val b = die.roll()
    if (a > b) 'GT
    else if (a < b) 'LT
    else 'EQ
  }
}

object Program extends App {
  val d61 = new Die(1 to 6)
  val d62 = new Die(1 to 6)

  for (_ <- 1 to 100)
    println(d61 rollAndCompareTo d62)
}
0

You may wish to simply pick fixed seeds that are different. Also, compat.Platform.currentTime() and System.currentTimeMillis() seem to be working fine (2014 Macbook Pro). You could try System.nanoTime() in case the two objects get instantiated within the same millisecond. But really, fixed seeds are better, e.g. for testing.

Also, this looks totally like Java - with mutable state, side effects, etc. If you're considering learning Scala the way it's encouraged to be used by its creators (as functional as possible), check out the book by Paul Chiusano and Rúnar Bjarnason called "Functional Programming in Scala" (Manning Press, early access, http://manning.com/bjarnason/). They have a whole chapter about purely functional state using an example of a random number generator. Or, check out NICTA's example: https://github.com/NICTA/rng

0

Consider java.security.SecureRandom which proves highly more unpredictable than java.util.Random based in linear congruential generators.

For a more detailed discussion note for instance https://stackoverflow.com/a/11052736/3189923 and http://docs.oracle.com/javase/8/docs/api/java/security/SecureRandom.html.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.