8

I have code that uses both RandomReal and RandomInteger a number of times to produces a sequence of $n$ data sets. After closing the notebook and quitting the kernel, I would like to be able to start the code where I left off in the random sequence, and generate an additional $m$ data sets.

I can do this with the old Random function and $RandomState. But, how do I do it with RandomReal and RandomInteger?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
user26718
  • 366
  • 1
  • 2
  • 6
    Probably with Random`GetRandomState and Random`SetRandomState. These are undocumented and might crash your kernel. I did get a crash with them yesterday. Related: http://mathematica.stackexchange.com/questions/91523/why-doest-seedrandom-work-here – Szabolcs Aug 14 '15 at 12:20
  • 1
    But is it really important for your application where you are exactly in the random sequence? Can you explain a bit more? – Szabolcs Aug 14 '15 at 12:21
  • 3
    start with SeedRandom, keep count of the random calls. Restart with same seed and make the right number of random calls to get back to the same point, – george2079 Aug 14 '15 at 12:40
  • 2
    @george, but if one was doing Monte Carlo with a few million or so calls to the PRNG before you lost patience, you'd hope there was a convenient way to pick up where you left off without going through that first few million again, no? For a congruential generator, this is not too hard to do, but of course nobody uses purely congruential generators in a nontrivial Monte Carlo application… – J. M.'s missing motivation Aug 14 '15 at 12:50
  • @J.M. I see that "jumping ahead" by precisely $n$ steps is discussed for many different generators. What is the practical use of this? – Szabolcs Aug 14 '15 at 13:28
  • @Szabolcs, apart from reproducible results (e.g. the Monte Carlo example I gave in my previous comment), I'm not aware of any other good use. FWIW, I've seen some of those papers too, but implementing their ideas in Mathematica was not so straightforward for me. – J. M.'s missing motivation Aug 14 '15 at 13:33
  • For each of your n steps initialize SeedRandom with s+i where i runs from 1...n. For each of your m steps initialize SeedRandom with s+i where i runs from n+1 to m. Where s is 'some seed'. – Ymareth Aug 14 '15 at 13:44
  • RandomInteger[]&/@Range[10^7]//Timing//First -> .55 – george2079 Aug 14 '15 at 15:53
  • @george2079 Why not use RandomInteger[{0, 1}, 10^7] which is an order of magnitude faster and leaves the randomizer at the same state as yours. – István Zachar Jul 13 '16 at 15:40

0 Answers0