From: Peter Perkins on 17 May 2010 07:44 On 5/16/2010 8:27 AM, shweta upadhya wrote: > can anyone plz explain the code..(function) > seed=9; > rand(state,seed); Just for the record: That syntax does two things, and doesn't or does do one additional things depending on what MATLAB version you're using: 1) it activates the VERY OLD multiplicative congruential uniform random number generator that was originally introduced in MATLAB V4 and deprecated in MATLAB V5. 2) it initializes that generator with the seed value 9, and every time you run that command, the RAND function will subsequently return exactly the same sequence of values. It is important to realize that the keywords 'seed' and 'state' in syntaxes like that are tied to specific generator algorithms, some of them old. 'seed', in this case, does not mean "reseed the current random number generator with 9", but rather means "activate the old V4 generator, and initialize it with 9 as the seed". Similarly, rand('state',9) does not mean, "initialize the current random number generator with the 9th state" (whatever that might mean), but rather, "activate the old V5 generator, and initialize it with 9 as the seed". 3a) if you are running in MATLAB prior to R2008b, rand('seed',9) does NOT affect the behavior of the RANDN function at all. 3b) if you're running in MATLAB R2008b or later, it activates the shift-register/congruential plus ziggurat normal random number generator underneath RANDN. You can see that by doing this: >> rand('seed',9) >> RandStream.getDefaultStream ans = legacy random stream (current default) RAND algorithm: V4 (Congruential) RANDN algorithm: V5 (Ziggurat) "Legacy" in this case indicates that MATLAB's random number generation is behaving as it did prior to R2008b, where RAND and RAND worked independently of each other. So: * if you want RAND to produce the specific sequence of numbers that you'll get after rand('seed',9), perhaps because you are generating input data for a benchmark test that you want to make sure always returns the same answer, go ahead and use rand('seed',9). You might consider that you've altered MATLAB's "global" random number settings, though. * if your fingers got used to typing rand('seed',9) over the years, and you're not really concerned with the statistical rigor of the random numbers the V4 algorithm generates, and don't care that the period of the V4 generator is fairly short (2^32), so ahead and use rand('seed',9). Ditto the above about the global effect. * but if you are trying to "get predictable results from MATLAB's random number generators, you should really be doing one of the following (you can use any integer, not just 9): - rand('twister',9) if you're using MATLAB prior to R2008b - reset(RandStream.getDefaultStream,9) in R2008b or later Note that the former selects a specific generator (the Mersenne Twister), while the latter resets whatever is the current random number generator. So if you've already activated one of the old generators using the pre-8b syntax in R2008b or later, you likely want to restart in a fresh MATLAB session, or do the following to reset things as if you had restarted: - RandStream.setDefaultStream(RandStream('mt19937ar','Seed',0)) Hope this helps.
First
|
Prev
|
Pages: 1 2 Prev: format a string in a listbox Next: simulink model parameters for building exe |