matlab中随机数生成函数rand()的使用方法
(2013-04-04 19:45:10)
Rand随机数
rand Uniformly distributed pseudorandom
numbers.
R = rand(N)
returns an N-by-N matrix containing pseudorandom values drawn
from the
standard uniform distribution on the open
interval(0,1). rand(M,N)
or
rand([M,N]) returns an M-by-N matrix.
rand(M,N,P,...) or
rand([M,N,P,...]) returns an M-by-N-by-P-by-...
array.
R=rand(N)返回一个N*N阶矩阵,R=rand(M*N)返回一个M*N阶的矩阵,同理可以返回一个M*N*P*Q。。。阶的矩阵,但是矩阵中的每一个元素都是取值于(0,1)的随机数
rand returns a
scalar. rand(SIZE(A)) returns an array the same
size as A.
Note: The
size inputs M, N, P, ... should be nonnegative integers.
Negative
integers are treated as 0.
R =
rand(..., 'double') or R = rand(..., 'single') returns an array
of
uniform
values of the specified class.
Compatibility Note: In versions of MATLAB prior to 7.7, you
controlled
the internal
state of the random number stream used by rand by calling
rand
directly with the 'seed', 'state', or 'twister'
keywords. That
syntax is
still supported for backwards compatibility, but is
deprecated.
Beginning in
MATLAB 7.7, use the default stream as described in
RANDSTREAM.
The sequence
of numbers produced by rand is determined by the internal
state of the
uniform pseudorandom number generator that underlies rand,
RANDI, and
RANDN. Control that default random number stream
using its
properties
and methods. See RANDSTREAM for details about the
default
stream.
Resetting
the default stream to the same fixed state allows
computations
to be
repeated. Setting the stream to different states
leads to unique
computations, however, it does not improve any statistical
properties.
Since MATLAB
uses the same state each time it starts up, rand, RANDN, and
RANDI will
generate the same sequence of numbers in each session unless
the state is
changed.
Examples:
Generate values from the uniform distribution on the interval [a,
b].
r = a + (b-a).*rand(100,1);
Generate integer values from the uniform distribution on the set
1:n.
r = randi(100,1);
Save the current state of the default stream, generate 5
values,
restore the state, and repeat the sequence.
defaultStream = RandStream.getDefaultStream;
savedState = defaultStream.State;
u1 = rand(1,5)
defaultStream.State = savedState;
u2 = rand(1,5) % contains exactly the same values as u1
Replace the default stream with a stream whose seed is based on
CLOCK, so
rand will return different values in different MATLAB
sessions. NOTE: It
is usually not desirable to do this more than once per MATLAB
session.
RandStream.setDefaultStream(RandStream('mt19937ar','seed',sum(100*clock)));
rand(1,5)
喜欢
0
赠金笔
加载中,请稍候......