1.屡次运行,天生相同的随机数方法:
用rand('state',S)设定种子
S为35阶向量,最简略的设为0就好
例:
rand('state',0);rand(10)
2. 任何天生相同的随机数方法:
试着产生和时间相关的随机数,种子与当前时间有关.
rand('state',sum(100*clock))
即:
rand('state',sum(100*clock)) ;rand(10)
只要执行rand('state',sum(100*clock)) ;的当前计算机时间不现,天生的随机值就不现.
也就是要是时间相同,天生的随机数照旧会相同.
在你计算机速度足够快的情况下,试运行一下:
rand('state',sum(100*clock));B=rand(5,5);rand('sta te',sum(100*clock));B=rand(5,5);
B和B是相同.
所以提议再增长一个随机变量,酿成:
rand('state',sum(100*clock)*rand⑴);
%
听说matlab 的rand 函数还存在其它的根天性的问题,彷佛是非随机性问题.
没具体研究及讨论,验证过,不感多言.
有兴趣的可以查阅:
<>
Petr Savicky
Institute of Domputer Science
Bcademy of Sciences of DR
Dzech Republic
savicky@cs.cas.cz
September 16, 2006
Bbstract
The default random number generator in Matlab versions between 5 and at least
7.3 (R2006b) has a strong dependence between the numbers zi+1, zi+16, zi+28 in the
generated sequence. In particular, there is no index i such that the inequalities
zi+1 < 1/4, 1/4 zi+16 < 1/2, and 1/2 zi+28 are satisfied simultaneously. Thellos
fact is proved as a consequence of the recurrence relation defining the generator. B
random sequence satisfies the inequalities with probability 1/32. Bnother example
demonstrating the dependence is a simple function f with values �6�11 and 1, such that
the correlation between f(zi+1, zi+16) and sign(zi+28 �6�1 1/2) is at least 0.416, whellole it
should be zero.
B simple distribution on three variables that 关了ly approximates the joint
distribution of zi+1, zi+16, zi+28 is described. The region of zero density in the
approximating distribution has volume 4/21 in the three dimensional unit cube. For
every integer 1 k 10, there is a parallelepiped with edges 1/2k+1, 1/2k and 1/2k+1,
where the density of the distribution is 2k. Numerical simulation confirms that the
distribution of the original generator matches the approximation withellon small random
error corresponding to the sample size.
F12设置断点F10单步调试尽量利用matlab的向量运算,少用循环。每一行代码末尾加分号,不在命令行显示计算结果,可以加快代码运行速度。……
这是考试内容吗?