Ask an expert. Trust the answer.

Your academic and career questions answered by verified experts

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


Why Matlabhelpers ?

Our Matlab assignment helpers for online MATLAB assignment help service take utmost care of your assignments by keeping the codes simple yet of high-quality. We offer the most reliable MATLAB solutions to students pursuing their Computer Science course from the Monash University, the University of Sydney, the University of New South Wales, the University of Melbourne; to name a few. Approach us today for best Matlab solutions online!

whatsApp order on matlabhelpers.com

telegram order on matlabsolutions.com