rand matlab程序代码如何看懂,matlab中rand的高级用法

相关半径:

对于一个区域变量,两个结点间其样本值的相关性随距离增大而减小。因此,可以确定一个距离,在这个距离之外,两结点间区域变量的样本值的相关性可以忽略不计,这个距离称为相关半径。

reset random number generator

Save the current state of the random number generator and create a 1-by-5 vector of random numbers.

s = rng;

r = rand(1,5)

r =

0.0975 0.2785 0.5469 0.9575 0.9649

Restore the state of the random number generator to s, and then create a new 1-by-5 vector of random numbers. The values are the same as before.

(产生和原来数值相等的随机数)

rng(s);

r1 = rand(1,5)

r1 =

0.0975 0.2785 0.5469 0.9575 0.9649

Always use the rng function (rather than the rand or randn functions) to specify the settings of the random number generator. For more information, see Replace Discouraged Syntaxes of rand and randn.

(使用rng函数,来设置随机数发生器,而不是用rand或者randn函数)

Replace Discouraged Syntaxes of rand and randn

On this page…

Description of the Former Syntaxes

Description of Replacement Syntaxes

Replacement Syntaxes for Initializing the Generator with an Integer Seed

Replacement Syntaxes for Initializing the Generator with a State Vector

If You Are Unable to Upgrade from Former Syntax

Description of the Former Syntaxes

In earlier versions of MATLAB®, you controlled the random number generator used by the rand and randn functions with the ‘seed’, ‘state’ or ‘twister’ inputs. For example:

(因版本不同,早起版本的设置)

rand(‘seed’,sd)

randn(‘seed’,sd)

rand(‘state’,s)

randn(‘state’,s)

rand(‘twister’,5489)

These syntaxes referred to different types of generators, and they will be removed in a future release for the following reasons:

The terms ‘seed’ and ‘state’ are misleading names for the generators.

All of the former generators except ‘twister’ are flawed.

They unnecessarily use different generators for rand and randn.

To assess the impact this change will have on your existing code, execute the following commands at the start of your MATLAB session:

warning(‘on’,’MATLAB:RandStream:ActivatingLegacyGenerators’)

warning(‘on’,’MATLAB:RandStream:ReadingInactiveLegacyGeneratorState’)

Description of Replacement Syntaxes

(使用rng函数)

Use the rng function to control the shared generator used by rand, randn, randi and all other random number generation functions like randperm, sprand, and so on. To learn how to use the rng function to replace the former syntaxes, take a few moments to understand what the former syntaxes did. This should help you to see which new rng syntax best suits your needs.

The first input to the former syntaxes of rand(Generator,s) or randn(Generator,s) specified the type of the generator, as described here.

Generator = ‘seed’ referred to the MATLAB v4 generator, not to the seed initialization value.

Generator = ‘state’ referred to the MATLAB v5 generators, not to the internal state of the generator.

Generator = ‘twister’ referred to the Mersenne Twister generator, now the MATLAB startup generator.

The v4 and v5 generators are no longer recommended unless you are trying to exactly reproduce the random numbers generated in earlier versions of MATLAB. The simplest way to update your code is to use rng. The rng function replaces the names for the rand and randn generators as follows.

rand/randn Generator Name rng Generator Name

‘seed’ ‘v4’

‘state’

‘v5uniform’ (for rand)

or

‘v5normal’ (for randn)

‘twister’ ‘twister’ (recommended)

Former Syntax Using rand/randn New Syntax Using rng

% Save v5 generator state.

st = rand(‘state’);

% Call rand.

x = rand;

% Restore v5 generator state.

rand(‘state’,st);

% Call rand again and hope

% for the same results.

y = rand

% Get generator settings.

s = rng;

% Call rand.

x = rand;

% Restore previous generator

% settings.

rng(s);

% Call rand again and % get the same results. y = rand

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值