0

I came across something in scala.util.Random that led me to unnecessary debugging yesterday.

I just needed random Doubles, and so used scala.util.Random. For similar seeds (i.e., integers 0 ... N ), I found that the first call to nextDouble() returned almost identical values; for my purposes of sampling a binned CDF, they were identical.

import scala.util.Random
val seed = 1 to 10
val nums = seed.map(u => new Random(u).nextDouble)

Result:

Vector(0.7308781907032909, 0.7311469360199058, 0.731057369148862, 0.7306094602878371, 0.730519863614471, 0.7307886238322471, 0.7306990420600421, 0.7302511331990172, 0.7301615514268123, 0.7304302967434272)

Is there something I'm not aware of about the scala.util.Random? Getting almost the same value for different seeds seems very strange to me.

Ben Reich
  • 16,222
  • 2
  • 38
  • 59
modulus0
  • 3,798
  • 1
  • 12
  • 10
  • 2
    This is essentially answered [here](http://stackoverflow.com/a/12284419/334519), since `scala.util.Random` is a very thin wrapper around `java.util.Random`. – Travis Brown Apr 22 '15 at 13:36
  • 1
    Also some good information here: http://stackoverflow.com/a/1554995/1223622 – Ben Reich Apr 22 '15 at 13:40
  • Thanks---I should have searched for the issue in Java too. – modulus0 Apr 22 '15 at 13:50
  • @modulus0 It's a reasonable question, especially since there's nothing in the `scala.util.Random` docs to indicate that it's just a wrapper around `java.util.Random`. – Travis Brown Apr 22 '15 at 15:22

0 Answers0