How to set custom seed for pseudo-random number generator
Date: 2022-07-07 11:31:07
I need to perform few tests where I use randn
pseudo random number generator. How can I set the seed on my own, so every time I run this test I will get the same results? (yeah, I know it's a little bit weird, but that's the problem).
I've found the RANDSTREAM
object that has the seed
property, but it's read only. Is there any way to use it for seeding the generator?
Answers:
The old way of doing it:
randn('seed',0)
The new way:
s = RandStream('mcg16807','Seed',0)
RandStream.setDefaultStream(s)
Note that if you use the new way, rand
and randn
share the same stream so if you are calling both, you may find different numbers being generated compared to the old method (which has separate generators). The old method is still supported for this reason (and legacy code).