Jan 15, 2013

Generate random number in old version Matlab


If you want to have different a sequence of random numbers when the program is run for multiple times, a seed has to be set. The function "rand('state', int)" can not do it.

The old version Matlab which is lower than v7.7 has no some common random number generators. You can not use RandStream or rng() to set the random seeds. So I use an extended random number generator which can be downloaded from http://people.sc.fsu.edu/~jburkardt/m_src/asa183/asa183.html (r8_random.m).

s1 = mod ( 171 * s1, 30269 );

s2 = mod ( 172 * s2, 30307 );

s3 = mod ( 170 * s3, 30323 );

r = mod ( s1 / 30269.0 ...

+ s2 / 30307.0 ...

+ s3 / 30323.0, 1.0 );

return



We just need to set different seed s1,s2,s3 at the beginning of your program each time when you run it and it will generate different random sequences.

No comments:

Post a Comment