3

How do I force the random number generator (if possible) with a specific seed in scala breeze, so say the following scala code always generate the same sequence seq?

    import breeze.stats.distributions._        
    val g = new Gaussian(0, 1)
    val seq = gau.sample(3)

I'm using scala to build a monte carlo simulator, and would like the simulation results to be repeatable (through parameterization of a specific random seed).

Carson Pun
  • 1,742
  • 2
  • 13
  • 20

1 Answers1

1

Create an implicit RandBasis and thread that through to wherever you create random generators. I should probably improve the API for this, but something like:

implicit val randBasis: RandBasis = new RandBasis(new ThreadLocalRandomGenerator(new MersenneTwister(seed)))
dlwh
  • 2,257
  • 11
  • 23
  • (that threading may not do what you want. each thread gets the same seed. You might want to use something that generates seeds based on some base seed and a (stable) indicator of thread identity.) – dlwh Oct 19 '15 at 23:11
  • Thanks Dave for the details, especially the parts on individual thread comment which I am going to tackle next. – Carson Pun Oct 20 '15 at 00:25
  • This doesn't seem to help if someone wants to control any random number generation that happens downstream inside blackbox breeze functions. For example, consider wanting to write a unit test that reproduces the exact same sequence of optimizer steps every time, just by setting up a random seed, some toy data, and invoking the optimizer functions. If the test operates at the level of calling optimizer functions like they are black boxes, then there is no option to "thread through" a RandBasis into the internals of those functions and whatever random structures they might maintain on their own. – ely Aug 13 '18 at 18:14