matlab随机函数rand使用中应注意的问题
2009-12-01 20:23
831 查看
matlab随机函数rand使用中应注意的问题(2009-04-06 15:49:33)
rand产生的是0到1(不包括1)的随机数.
matlab的rand函数生的是伪随机数,即由种子递推出来的,相同的种子,生成相同的随机数.
matlab刚运行起来时,种子都为初始值,因此每次第一次执行rand得到的随机数都是相同的.
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));A=rand(5,5);rand('state',sum(100*clock));B=rand(5,5);
A和B是相同.
所以建议再增加一个随机变量,变成:
rand('state',sum(100*clock)*rand(1));
%
据说matlab 的rand 函数还存在其它的根本性的问题,似乎是非随机性问题.
没具体研究及讨论,验证过,不感多言.
有兴趣的可以查阅:
<<A strong nonrandom pattern in Matlab default random number generator>>
Petr Savicky
Institute of Computer Science
Academy of Sciences of CR
Czech Republic
savicky@cs.cas.cz
September 16, 2006
Abstract
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. This
fact is proved as a consequence of the recurrence relation defining the generator. A
random sequence satisfies the inequalities with probability 1/32. Another example
demonstrating the dependence is a simple function f with values −1 and 1, such that
the correlation between f(zi+1, zi+16) and sign(zi+28 − 1/2) is at least 0.416, while it
should be zero.
A simple distribution on three variables that closely 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 within small random
error corresponding to the sample size.
标签:杂谈 |
matlab的rand函数生的是伪随机数,即由种子递推出来的,相同的种子,生成相同的随机数.
matlab刚运行起来时,种子都为初始值,因此每次第一次执行rand得到的随机数都是相同的.
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));A=rand(5,5);rand('state',sum(100*clock));B=rand(5,5);
A和B是相同.
所以建议再增加一个随机变量,变成:
rand('state',sum(100*clock)*rand(1));
%
据说matlab 的rand 函数还存在其它的根本性的问题,似乎是非随机性问题.
没具体研究及讨论,验证过,不感多言.
有兴趣的可以查阅:
<<A strong nonrandom pattern in Matlab default random number generator>>
Petr Savicky
Institute of Computer Science
Academy of Sciences of CR
Czech Republic
savicky@cs.cas.cz
September 16, 2006
Abstract
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. This
fact is proved as a consequence of the recurrence relation defining the generator. A
random sequence satisfies the inequalities with probability 1/32. Another example
demonstrating the dependence is a simple function f with values −1 and 1, such that
the correlation between f(zi+1, zi+16) and sign(zi+28 − 1/2) is at least 0.416, while it
should be zero.
A simple distribution on three variables that closely 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 within small random
error corresponding to the sample size.
相关文章推荐
- C中随机函数rand()使用时要注意的问题
- 使用matlab计算DFT时需要注意的幅值对应问题
- matlab使用reshape应该注意的问题
- matlab使用reshape应该注意的问题
- matlab使用reshape应该注意的问题
- OpenGL使用模板模板缓冲时需要注意的几个问题
- 关于MATLAB中使用的UBIT1问题
- replaceAll、replaceFirst使用需要注意的问题
- 使用eval()解析JSON格式字符串应注意的问题
- Template 使用注意问题和范例
- Hibernate使用二级缓存时,createSQLQuery需要注意的问题
- Hibernate使用二级缓存时,createSQLQuery需要注意的问题
- jquery在IE中使用append应注意的问题
- 使用Mount bind时需要注意的权限问题
- 使用Matlab相机标定库(Camera Calibration Toolbox)问题小记
- [置顶] 基于SIFT+Kmeans+SVM的场景识别,参数需注意的问题(Matlab实现)
- Cygwin使用过程中需要注意的一些问题
- 解释下带xmlns的xml 使用Xpath时需要注意的问题
- java实际开发中泛型使用需要注意的一些问题
- java语言中使用三元式的时候应该注意的问题