Quantcast

Documentation Center

  • Trial Software
  • Product Updates

rand

Uniformly distributed pseudorandom numbers

Syntax

  • r = rand
  • r = rand(n)
  • r = rand(sz1,...,szN)
  • r = rand(sz)
  • r = rand(classname)
  • r = rand(n,classname)
  • r = rand(sz1,...,szN,classname)
  • r = rand(sz,classname)
  • r = rand('like',p)
  • r = rand(n,'like',p)
  • r = rand(sz1,...,szN,'like',p)
  • r = rand(sz,'like',p)

Description

r = rand returns a pseudorandom scalar drawn from the standard uniform distribution on the open interval (0,1).

r = rand(n) returns an n-by-n matrix of pseudorandom uniform values.

r = rand(sz1,...,szN) returns a sz1-by-...-by-szN array of pseudorandom uniform values where sz1,...,szN indicates the size of each dimension. For example, rand(3,4) returns a 3-by-4 array of pseudorandom values.

r = rand(sz) is an array of pseudorandom uniform values where the size vector, sz, defines size(r). For example, rand([3,4]) returns a 3-by-4 array of pseudorandom values.

    Note   The size inputs sz1,...,szN, as well as the elements of the size vector sz, should be nonnegative integers. Negative integers are treated as 0.

r = rand(classname) returns a pseudorandom uniform value where the string, classname, specifies the data type. classname can be either 'single' or 'double'.

r = rand(n,classname) returns an n-by-n array of pseudorandom uniform values of data type classname.

r = rand(sz1,...,szN,classname) returns a sz1-by-...-by-szN array of pseudorandom uniform values of data type classname.

r = rand(sz,classname) returns an array of pseudorandom uniform values where the size vector, sz, defines size(r) and classname defines class(r).

r = rand('like',p) returns a pseudorandom uniform value of the same data type as the numeric variable, p.

r = rand(n,'like',p) returns an n-by-n array of pseudorandom uniform values like p.

r = rand(sz1,...,szN,'like',p) returns a sz1-by-...-by-szN array of pseudorandom uniform values like p

r = rand(sz,'like',p) returns an array of pseudorandom uniform values like p where the size vector, sz, defines size(r).

The sequence of numbers produced by rand is determined by the internal settings of the uniform random number generator that underlies rand, randi, and randn. You can control that shared random number generator using rng.

Examples

Example 1

Generate values from the uniform distribution on the interval [a, b]:

 r = a + (b-a).*rand(100,1);

Example 2

Use the randi function, instead of rand, to generate integer values from the uniform distribution on the set 1:100:

r = randi(100,1,5);

Example 3

Reset the random number generator used by rand, randi, and randn to its default startup settings, so that rand produces the same random numbers as if you restarted MATLAB®:

rng('default')
rand(1,5)
ans =
    0.8147    0.9058    0.1270    0.9134    0.6324

Example 4

Save the settings for the random number generator used by rand, randi, and randn, generate 5 values from rand, restore the settings, and repeat those values:

s = rng;
u1 = rand(1,5)
u1 =
    0.0975    0.2785    0.5469    0.9575    0.9649

rng(s);
u2 = rand(1,5)
u2 =
    0.0975    0.2785    0.5469    0.9575    0.9649

u2 contains exactly the same values as u1.

Example 5

Reinitialize the random number generator used by rand, randi, and randn with a seed based on the current time. rand returns different values each time you do this. Note that it is usually not necessary to do this more than once per MATLAB session as it may affect the statistical properties of the random numbers MATLAB produces:

rng('shuffle');
rand(1,5);

See Also

| | | | | | |

Related Examples

Was this topic helpful?