How to set custom seed for pseudo-random number generator

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).

See http://www.mathworks.com/help/techdoc/math/bsn94u0-1.html for more info.

Leave a Comment