matlab中用rng替换rand('seed',sd)、randn('seed',sd)和rand('state',sd)的通俗解释

1. rand('seed',sd)、randn('seed',sd)和rand('state',sd)中各参数的含义

      我们知道,rand和randn都是用来产生伪随机数的,但是产生伪随机数的generator(发生器)有多种,而seed、state、twister等就是用来指定不同类型的伪随机数发生器的,其中seed 指“v4 generator”,state指“v5 generator”,twister指"Mersenne Twister generator"。第二个参数“sd”是“seed”的缩写,表示发生器的种子点,这个值将作为generator的输入,用于产生伪随机数。这里有必要先解释一下随机数和伪随机数的本质区别:随机数是在某次产生过程中,按照实验过程中表现的分布概率随机产生的,其结果是不可预测的,不可重复的;而伪随机数则是按照一定算法模拟产生的,其结果是确定的,可重复的。当然这里的确定和可重复是指给定相同的初始值,算法模拟将产生相同的输出。

2. 为什么要用rand('seed',sd)、randn('seed',sd)和rand('state',sd)

        在实际应用中,比如检查程序问题时,为了重现错误,我们可能希望产生相同的伪随机数,这个时候就需要用这些表达式了。正如前面所说的,如果选用相同的generator并设置相同的初始值sd,那么你就可以得到同样伪随机数。

举个例子:

rand('seed',0); %选定generator并设定初始值

a=rand(3,3);

rand('seed',0);%设定同样的generator和相同的初始值

b=rand(3,3);

a

b

输出:

a =
   0.218959186328090   0.679296405836612   0.519416372067955  
   0.047044616214486   0.934692895940828   0.830965346112366 
   0.678864716868319   0.383502077489859   0.034572110527461 

b =
   0.218959186328090   0.679296405836612   0.519416372067955  
   0.047044616214486   0.934692895940828   0.830965346112366  
   0.678864716868319   0.383502077489859   0.034572110527461   

对比后你会发现a和b相等。

3. 为什么要用rng替换rand('seed',sd)、randn('seed',sd)和rand('state',sd)

        MATLAB官方给出的理由如下:

1)“seed”和“state”等词对于所对应的generator来说是一种误导;

2)除了“twister”以外,其他的generator都有问题;

3)这些表达式对于rand和randn采用了不同的generator,没必要。

所以在新版的MATLAB中已经不推荐这种方法了。

4. 替换的具体方法

下图是MATLAB给的一个替换的方式,最右边的才是它推荐的方法。


实际应用中举例如下(这里只以seed为例,其他的类似):

randn('seed',0);%rng(0);

a=randn(4,3);

randn('seed',0);%rng(0);

b=randn(4,3);

a

b

MATLAB推荐用注释掉的语句替换对应行的语句。

  • 12
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
% Known encoding formats are the following FDSN codes: % 0: ASCII % 1: 16-bit integer % 2: 24-bit integer (untested) % 3: 32-bit integer % 4: IEEE float32 % 5: IEEE float64 % 10: Steim-1 % 11: Steim-2 % 12: GEOSCOPE 24-bit (untested) % 13: GEOSCOPE 16/3-bit gain ranged % 14: GEOSCOPE 16/4-bit gain ranged (untested) % 19: Steim-3 (alpha and untested) % % See also MKMSEED to export data in miniSEED format. % % % Author: Franois Beauducel % Institut de Physique du Globe de Paris % Created: 2010-09-17 % Updated: 2012-04-21 % % Acknowledgments: % Ljupco Jordanovski, Jean-Marie Saurel, Mohamed Boubacar, Jonathan Berger, % Shahid Ullah. % % References: % IRIS (2010), SEED Reference Manual: SEED Format Version 2.4, May 2010, % IFDSN/IRIS/USGS, http://www.iris.edu % Trabant C. (2010), libmseed: the Mini-SEED library, IRIS DMC. % Steim J.M. (1994), 'Steim' Compression, Quanterra Inc. % History: % [2012-04-21] % - Correct bug with Steim + little-endian coding % (thanks to Shahid Ullah) % [2012-03-21] % - Adds IDs for warning messages % [2011-11-10] % - Correct bug with multiple channel name length (thanks to % Jonathan Berger) % [2011-10-27] % - Add LocationIdentifier to X.ChannelFullName % [2011-10-24] % - Validation of IEEE double encoding (with PQL) % - Import/plot data even with file integrity problem (like PQL) % [2011-07-21] % - Validation of ASCII encoding format (logs) % - Blockettes are now stored in substructures below a single % field X.BLOCKETTES % - Add import of blockettes 500 and 2000 % - Accept multi-channel files with various data coding % [2010-10-16] % - Alpha-version of Steim-3 decoding... % - Extend output parameters with channel detection % - Add gaps and overlaps on plots % - Add possibility to force the plot % [2010-10-02] % - Add the input formats for GEOSCOPE multiplexed old data files % - Additional output argument with gap and overlap analysis % - Create a plot when no output argument are specified % - Optimize script coding (30 times faster STEIM decoding!) % % [2010-09-28] % - Correction of a problem with STEIM-1 nibble 3 decoding (one % 32-bit difference) % - Add reading of files without blockette 1000 with additional % input arguments (like Seismic Handler output files). % - Uses warning() function instead of fprintf().

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值