MATLAB - Random Numbers



Random numbers are numbers that are selected randomly from a large set of numbers. The computer generated random numbers are called pseudorandom numbers.The pseudorandom number generation makes use of an algorithm that internally makes use of mathematical formulas that generates a bits of random digits which are unpredictable.The pseudorandom numbers are useful in computer applications like games,cryptography etc.

Matlab gives a lot of functions like rand, randn, and randi that can generate random numbers. For advanced level random numbers we can make use of the class RandStream.

Another function randperm creates random permutation of integers.

Let us work on each of the functions with examples to understand it better.

Working with rand function

rand − this function will return random numbers that are uniformly distributed. The syntax for the function is as discussed below.

Syntax

a = rand
a = rand(n)
a = rand(sz1, sz2,.....szn)
a = rand(sz)
a = rand(_, typename)
a = rand(_, like, p)
a = rand(s,_)
Sr.No Syntax & Description
1

a = rand

This will return a random number between 0 to 1.

2

a = rand(n)

This will return a random number that is uniformly distributed. It will be a matrix of nxn.

3

a = rand(sz1, sz2,.....szn)

This will return a uniformly distributed array of random numbers of the size sz1 by szn.

For example rand(2,3) returns a matrix of size 2×3. So the random number array will be of size 2×3.

4

a = rand(sz)

Here the vector size is sz. The function will return a random number array of size sz. For example rand([2,3]) will return an array of size 2×3.

5

a = rand(_, typename)

The array of random numbers returned here is of type typename.For example single, double.

6

a = rand(_, like, p)

This will give you an array of random numbers that have a datatype like p or complexity (real or complex) like p. You can make use of typename or like but not both together.

7

a = rand(s,_)

The random numbers are created from the stream s. You can make use of RandStream to create a stream.

Let us see examples for each of the syntax we have discussed above.

Example 1

a = rand

On execution the output is as follows −

a = 0.9584

In the example we are just calling the rand function. The value is stored inside the variable a.

The value given is 0.9584, the value generated lies between 0 to 1.

Let us call the rand function again.

a= rand

On execution the output is as follows −

a = 0.075312

Using the same function again the value is now 0.075312.

Example 2

In this example I will try to call rand using a n value.

a = rand(4)

On execution the output is as follows −

a =

   0.750825   0.930217   0.168903   0.781070
   0.732294   0.017075   0.545846   0.483156
   0.609990   0.311062   0.415960   0.369801
   0.871756   0.556813   0.401244   0.225472

The number we have passed to rand is 4. So it returns a matrix of size 4x4 with random numbers as shown above.

Now let us make use of number 2 and see the matrix of 2x2 as shown below −

a = rand(2)

On execution the output is as follows −

a =

   0.8271   0.5491
   0.9997   0.1035

Example 3

In this example we will pass the nxn matrix to the rand function.

a = rand(3,3)

On execution the output is as follows −

a =

   0.5684   0.3099   0.4883
   0.7016   0.8126   0.9140
   0.2272   0.4579   0.8909

The matrix generated is of size 3x3.

Let us check on another example with a 2x4 matrix.

a = rand(2,4)

On execution the output is as follows −

a =

   0.5362   0.6302   0.8595   0.4848
   0.3148   0.1463   0.6696   0.5108

Example 4

In this example will make use of vector array of size [3,4] inside the rand function as shown below

a =  rand([3,4])

On execution the output is as follows −

a =

   8.6458e-01   4.6462e-03   6.8987e-01   5.7768e-01
   7.2335e-01   2.8741e-01   3.4124e-01   7.6021e-02
   2.8896e-01   6.9273e-01   4.4906e-01   8.4528e-01

The output is a 3x4 matrix.

Let us try another example with a [2,2] vector array.

a = rand([2,2])

On execution the output is as follows −

a =

   0.6734   0.4422
   0.4438   0.3153

Example 5

In this example will pass the typename and see the output of the random number generated.

a = rand(3,"single")

On execution the output is as follows −

a =

   0.124537   0.265118   0.110287
   0.395455   0.102573   0.826784
   0.062113   0.124306   0.141749

The first argument is 3 , so it will create a 3x3 matrix of a single typename.

Let us see another example

a = rand([2,3], "double")

On execution the output is as follows −

a =

   0.078242   0.617494   0.702426
   0.721792   0.878933   0.199488

In the above example we are passing a vector [2,3] that generates a 2x3 matrix of type double.

Example 6

In this example will first create a matrix of size 2x2 which is of type single precision.

p = single([3 2; -2 1])

On execution the output is as follows −

p =

   3   2
  -2   1

Now let us make use of the p in the rand function as shown below

a = rand(size(p),"like",p)

On execution the output is as follows −

a =

  22 single matrix

    0.1966    0.6160
    0.2511    0.4733

It gives back a matrix of random numbers of size 2x2.

Example 7

In this example let us create a stream first as shown below −

s = RandStream('dsfmt19937')

Now let us make use of the stream s inside the rand function as shown below −

a = rand(s,[3 3])

Working with randn function

randn − this function is the same as rand but the only difference is that it will return random numbers that are normally distributed. The syntax for the function is as discussed below.

Syntax

a = randn
a = randn(n)
a = randn(sz1, sz2,.....szn)
a = randn(sz)
a = randn(_, typename)
a = randn(_, like, p)
a = randn(s,_)
Sr.No Syntax & Description
1

a = randn

This will return a random scalar number from standard normal distribution.

2

a = rand(n)

This will return a nxn matrix that comes from standard normal distribution.

3

a = rand(sz1, sz2,.....szn)

This will return an array of random numbers of the size sz1 by szn.

For example rand(2,3) returns a matrix of size 2x3. So the random number array will be of size 2x3.

4

a = rand(sz)

Here the vector size is sz. The function will return a random number array of size sz. For example rand([2,3]) will return an array of size 2x3.

5

a = rand(_, typename)

The array of random numbers returned here is of type typename.For example single, double.

6

a = rand(_, like, p)

This will give you an array of random numbers that have a datatype like p or complexity (real or complex) like p. You can make use of typename or like but not both together.

7

a = rand(s,_)

The random numbers are created from the stream s. You can make use of RandStream to create a stream.

Let us see examples for each of the syntax we have discussed above.

Example 1

a = randn

On execution the output is as follows −

a = -0.1226

In the example we are just calling the randn function. The value is stored inside the variable a.

The value given is 0.5377, the value generated from standard normal distribution i.e it will have mean value of 0 and standard deviation as 1.

Example 2

In this example I will try to call randn using a n value.

a = randn(2)

On execution the output is as follows −

a =

   0.1708  -0.9527
   1.8627   2.8103

The number we have passed to randn is 2. So it returns a matrix of size 2x2 with random numbers as shown above.

Example 3

In this example we will pass the nxn matrix to the rand function.

a = randn(3,3)

On execution the output is as follows −

a =

   0.067762   0.519480  -0.568026
   0.267630   0.232140  -1.267290
   0.808611   1.462018   1.039429

The matrix generated is of size 3x3.

Example 4

In this example will make use of vector array of size [3,4] inside the randn function as shown below

a = randn([3,4])

On execution the output is as follows −

a =

  -0.2043  -0.4831  -0.4446   0.4228
   0.2926   2.2891   1.5870   0.3228
   0.4624  -1.0989   1.2035  -0.4612

The output is a 3x4 matrix.

Example 5

In this example will pass the typename and see the output of the random number generated.

a = randn(3,"single")

On execution the output is as follows −

a =

   1.6072  -0.4121   0.2886
   0.7244   1.3422   0.9589
   0.9034   0.6775  -0.5273

The first argument is 3 , so it will create a 3x3 matrix of a single typename.

Example 6

In this example will first create a matrix of size 2x2 which is of type single precision.

p = single([3 2; -2 1])

On execution the output is as follows −

p =

   3   2
  -2   1

Now let us make use of the p in the rand function as shown below

a = randn(size(p),"like",p)

It gives back a matrix of random numbers of size 2x2.

Example 7

In this example let us create a stream first as shown below −

s = RandStream('dsfmt19937')

Now let us make use of the stream s inside the randn function as shown below

a = randn(s,[3 3])

Working with randi function

randi − Deals with uniformly distributed pseudo random integers.

Syntax

a = randi(imax)
a = randi(imax,n)
a = randi(imax,sz1,...,szN)
a = randi(imax,sz)
a = randi(___,typename)
a = randi(___,"like",p)
a = randi([imin,imax],___)
a = randi(s,___)
Sr.No Syntax & Description
1

a = randi(imax)

This will return a pseudorandom scalar integer that is between 0 to imax.

2

a = randi(imax,n)

This will return a nxn matrix with integers that falls between [1, imax].

3

a = randi(imax,sz1,...,szN)

This will return an array of random integers of the size sz1 by szn.

For example randi(10, 2,3) returns a matrix of size 2x3 pseudo random integers between 1 to 10.

4

a = randi(imax,sz)

Here the vector size is sz. The function will return a random integer array of size sz. For example randi(10, [2,3]) will return an array of size 2x3 with random integers from 1 to 10.

5

a = randi(_, typename)

The array of random integers between 1 to imax of type typename.For example "single", "double", "int8", "uint8", "int16", "uint16", "int32", "uint32", or "logical".

6

a = rand(_, like, p)

This will give you an array of pseudo random integers like p or complexity (real or complex) like p. You can make use of typename or like but not both together.

7

a = randi([imin,imax],___)

This will return an array of pseudorandom integers in an interval of imin and imax.

8

a = rand(s,_)

The random numbers are created from the stream s. You can make use of RandStream to create a stream.

Example 1

In this example let us see the output of a = randi(imax)

a = randi(6)

On execution the output is as follows −

a = 2

Example 2

In this example we will see the output that we get for a = randi(imax,n).

a = randi(5,3)

On execution the output is as follows −

a =

   5   1   5
   1   4   1
   5   2   5

Example 3

Let us check the output that we get for a = randi(imax,sz1,...,szN)

Here imax will use 8 and the matrix size will be 2,3.

a = randi(8,2,3)

On execution the output is as follows −

a =

   7   4   2
   2   4   5

Example 4

Let us check the output that we get for a = randi(imax,sz)

Here imax will use 8 and the matrix size will be [2,3].

a = randi(8,[2,3])

On execution the output is as follows −

a =

   6   7   5
   2   7   3

Example 5

In this example will pass the typename and see the output of the random integers generated.

a = randi(5, "int32")

On execution the output is as follows −

a = 3

Example 6

In this example will pass the typename and see the output of the random integers generated.

a = randi(10,size(p),"like",p)

Example 7

In this example let us check the pseudo random integers in an interval of imin and imax.

r = randi([1,5],3,3)  

On execution the output is as follows −

r =

   3   2   3
   4   1   1
   1   4   1

The imin is 1 and imax is 5. The matrix of size 3x3 is generated from the interval [1,5].

Example 8

In this example let us create a stream first as shown below −

s = RandStream('dsfmt19937')

The stream is used in the randi function as shown below.

a = randi(s,[1,10],[2 3])

Working with randperm function

randperm − The random number is generated by random permutation of integers.

Syntax

a = randperm(n)
a = randperm(n,k)
a = randperm(s,___)
Sr.No Syntax & Description
1

a = randperm(n)

This will return a row vector that will have integers that are permutation of from 1 to n and will not repeat any values.

2

a = randperm(n,k)

This will return a row vector that will have unique k integers which are randomly selected from the permutation done from 1 to n.

3

a = randperm(s,___)

The random permutation generates integers that are done on the random number stream s.

Example 1

In this example will see the output generated using a = randperm(n). Will use n as 10. So the permutation will take place from 1 to 10.

a = randperm(10)

On execution the output is as follows −

a =

    8    7    5    4   10    6    2    1    3    9

Example 2

Let us check the row vector of k unique integers returned from the function a = randperm(n,k). Where k = 5 and n= 10.

a = randperm(10,5)

On execution the output is as follows −

a =

    1    2   10    5    4

Example 3

In this example let us use stream for permutation.

s = RandStream('dsfmt19937')

Now let us use the stream inside a = randperm(s,___).

a = randperm(s,6)
Advertisements