- •In matlab 7.7, use the default stream as described in randstream.
- •Vector and covariance matrix.
- •Is usually not desirable to do this more than once per matlab session.
- •Independent random number streams, use RandStream.Create.
- •Is usually not desirable to do this more than once per matlab session.
- •Initialize all streams. Default is 0.
matlab\randfun - Random matrices and random streams
Random matrices.
<rand> - Uniformly distributed random numbers.
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. 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);
Use the RANDI function to generate to generate one integer value from the
uniform distribution on the set 1:100.
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)
See also randi, randn, RandStream, RandStream/rand, RandStream/getDefaultStream,
sprand, sprandn, randperm.
Overloaded methods:
RandStream/rand
distributed/rand
codistributor2dbc/rand
codistributor1d/rand
codistributed/rand
Reference page in Help browser
doc rand
<randi> - Uniformly distributed random integers.
RANDI Pseudorandom integers from a uniform discrete distribution.
R = RANDI(IMAX,N) returns an N-by-N matrix containing pseudorandom
integer values drawn from the discrete uniform distribution on 1:IMAX.
RANDI(IMAX,M,N) or RANDI(IMAX,[M,N]) returns an M-by-N matrix.
RANDI(IMAX,M,N,P,...) or RANDI(IMAX,[M,N,P,...]) returns an
M-by-N-by-P-by-... array. RANDI(IMAX) returns a scalar.
RANDI(IMAX,SIZE(A)) returns an array the same size as A.
R = RANDI([IMIN,IMAX],...) returns an array containing integer
values drawn from the discrete uniform distribution on IMIN:IMAX.
Note: The size inputs M, N, P, ... should be nonnegative integers.
Negative integers are treated as 0.
R = RANDI(..., CLASSNAME) returns an array of integer values of class
CLASSNAME.
The sequence of numbers produced by RANDI is determined by the internal
state of the uniform pseudorandom number generator that underlies RAND,
RANDI, and RANDN. RANDI uses one uniform value from that default
stream to generate each integer value. Control the default 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 integer values from the uniform distribution on the set 1:10.
r = randi(10,100,1);
Generate an integer array of integers drawn uniformly from 1:10.
r = randi(10,100,1,'uint32');
Generate integer values drawn uniformly from -10:10.
r = randi([-10 10],100,1);
Save the current state of the default stream, generate 5 integer
values, restore the state, and repeat the sequence.
defaultStream = RandStream.getDefaultStream;
savedState = defaultStream.State;
i1 = randi(10,1,5)
defaultStream.State = savedState;
i2 = randi(10,1,5) % contains exactly the same values as i1
Replace the default stream with a stream whose seed is based on CLOCK, so
RANDI 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)));
randi(10,1,5)
See also rand, randn, RandStream, RandStream/randi, RandStream/getDefaultStream.
Overloaded methods:
RandStream/randi
Reference page in Help browser
doc randi
<randn> - Normally distributed random numbers.
RANDN Normally distributed pseudorandom numbers.
R = RANDN(N) returns an N-by-N matrix containing pseudorandom values drawn
from the standard normal distribution. RANDN(M,N) or RANDN([M,N]) returns
an M-by-N matrix. RANDN(M,N,P,...) or RANDN([M,N,P,...]) returns an
M-by-N-by-P-by-... array. RANDN returns a scalar. RANDN(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 = RANDN(..., 'double') or R = RANDN(..., 'single') returns an array of
normal 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 RANDN by calling
RANDN directly with the 'seed' or 'state' 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 RANDN is determined by the internal
state of the uniform pseudorandom number generator that underlies RAND,
RANDI, and RANDN. RANDN uses one or more uniform values from that
default stream to generate each normal value. Control the default
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 a normal distribution with mean 1 and standard
deviation 2.
r = 1 + 2.*randn(100,1);
Generate values from a bivariate normal distribution with specified mean
Vector and covariance matrix.
mu = [1 2];
Sigma = [1 .5; .5 2]; R = chol(Sigma);
z = repmat(mu,100,1) + randn(100,2)*R;
Save the current state of the default stream, generate 5 values,
restore the state, and repeat the sequence.
defaultStream = RandStream.getDefaultStream;
savedState = defaultStream.State;
z1 = randn(1,5)
defaultStream.State = savedState;
z2 = randn(1,5) % contains exactly the same values as z1
Replace the default stream with a stream whose seed is based on CLOCK, so
RANDN 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)));
randn(1,5)
See also rand, randi, RandStream, RandStream/randn, RandStream/getDefaultStream.
Overloaded methods:
RandStream/randn
distributed/randn
codistributor2dbc/randn
codistributor1d/randn
codistributed/randn
Reference page in Help browser
doc randn
Random reordering.
<randperm> - Random permutation.
RANDPERM Random permutation.
RANDPERM(n) is a random permutation of the integers from 1 to n.
For example, RANDPERM(6) might be [2 4 5 6 1 3].
Note that RANDPERM calls RAND and therefore changes RAND's state.
See also permute.
Overloaded methods:
RandStream/randperm
Reference page in Help browser
doc randperm
Random Streams
<RandStream> - Create a random number stream.
RANDSTREAM Random number stream.
(Pseudo)random numbers in MATLAB come from one or more random number
streams. The simplest way to generate arrays of random numbers is to use
RAND, RANDN, or RANDI. These functions all rely on the same stream of
uniform random numbers, known as the default stream. You can create other
streams that act separately from the default stream, and you can use their
RAND, RANDN, or RANDI methods to generate arrays of random numbers. You can
also create a random number stream and make it the default stream.
To create a single random number stream, use either the RandStream
constructor or the RandStream.CREATE factory method. To create multiple