In this series of posts, I will explain how to use the various random number generation functions in Matlab. This will include the usage of the basic commands, how to control random number generation, how to create other distributions from the basic functions that Matlab provides, and what alternatives there are to the functions used in Matlab. In this post, I will explain the basic random number generation commands in Matlab, including rand, randn, randi, and randperm, and provide some example applications.

## Usage of basic commands

Matlab has the capability of producing pseudorandom numbers for use in numerical computing applications. The basic suite of random-number-generating functions includes rand, randn, randi, and randperm. In this section, we will give a brief overview of each of these functions. The function rand generates pseudorandom numbers with a uniform distribution over the range of (0, 1). Below are two examples.

>> u = rand(3) %Generates a 3x3 matrix u = 0.8147 0.9134 0.2785 0.9058 0.6324 0.5469 0.1270 0.0975 0.9575 >> u = rand(1,5) %Generates a 1x5 matrix u = 0.9649 0.1576 0.9706 0.9572 0.4854

The uniform distribution is commonly used to generate random numbers over an interval. This can be accomplished with the following code for an interval (a, b) whose output is a multidimensional matrix of size m x n x p ….

>> u = a + (a-b)*rand(m,n,p,...)

The function randn generates psueudorandom numbers with a normal (Gaussian) distribution with mean zero and unit variance, abbreviated as N(0, 1). This distribution is quite common in nature and is used in a wide variety of scientific, mathematical, and engineering applications, which justifies its own implementation in Matlab.

>> g = randn(2,3) %Generates a 2x3 matrix g = 0.7147 -0.1241 1.4090 -0.2050 1.4897 1.4172 >> M = ones(1,4); >> g = randn(size(M)) %Generates a matrix of the same size as M with normally distributed values g = 0.6715 -1.2075 0.7172 1.6302

A common use of this function is to create a vector of normally distributed values with a specified mean and variance.

>> m = 10; %Mean >> v = 9; %Variance >> d = 1000; %Length >> g = m + sqrt(v)*randn(d,1); >> mean(g) %Mean ans = 9.8529 >> var(g) %Variance ans = 8.6272 >> std(g) %Standard Deviation ans = 2.9372

Alternatively, the standard deviation can be used.

>> s = 3; %Standard deviation >> g = m + s*randn(d,1); >> var(g) %Variance ans = 9.0355 >> std(g) %Standard Deviation ans = 3.0059

The randi function generates a matrix of pseudorandom integers over a specified range. The following command creates a matrix of random integers of size m x n in a range from 1 to x. Unlike rand and randn, a parameter specifying the range must be entered before the dimensions of the matrix. In the following example, a 2 x 4 matrix of random integers in the range of [1, 10] is created.

>> b = randi(x, m, n); >> b = randi(10, 2, 4) b = 9 4 4 10 2 5 8 2

This command creates integers in the range from x to y, where x < y.

>> b = randi([x y], m, n)

This function is useful for selecting a random index of a vector or matrix. In the first example, randi is used as an index of the row vector of [0.1, 0.2, …, 1.0]. In the second example, a random index in a 10 x 5 x 2 matrix of random numbers is selected with the use of three randi commands, one for each dimension.

>> v = 0.1:0.1:1; >> randv = v(randi(length(v))) randv = 0.9000 >> A = rand(10,5,2); >> sA = size(A); >> randA = A(randi(sA(1)),randi(sA(2)),randi(sA(3))) randA = 0.6520

The randperm function is slightly different from the previous three functions in that instead of generating a random number, it generates a random permutation of a list of integers in the range of [1, n].

>> b = randperm(10) b = 1 9 10 5 7 2 4 3 6 8

Adding another parameter allows one to obtain a row vector containing unique, randomly selected integers from a list. Note that the results of randperm differ from those of randi. The randperm function performs sampling without replacement, whereas the randi function performs sampling with replacement.

>> b = randperm(10,3) b = 7 4 8 >> c = randi(10,1,3) c = 7 2 2

The randperm function can be used in “shuffling” and “dealing” applications, as demonstrated below. The first command creates a deck of four cards. The second command shuffles that deck, and the third command deals a hand of two randomly selected cards from the original deck.

>> deck = {'One','Two','Three','Four'}; >> shuffleddeck = deck(randperm(length(deck))) shuffleddeck = 'Three' 'Two' 'Four' 'One' >> hand = deck(randperm(length(deck),2)) hand = 'Two' 'Four'

This concludes our introduction to random number generation in Matlab. In the next post, I will explain how to control these functions and what some alternatives for Matlab’s random functions are for projects with stronger requirements for random numbers, such as cryptography.

Morning…. I would like to know what mod(s,10000000) means.

how to generate a matrix with predefined probability of occurrence for each possible value?

for example,i need to generate a 3×5 matrix consisting of only +1,0,-1, where

the probability of occurrence of +1=0.25

the probability of occurrence of -1=0.25 and

the probability of occurrence of 0=0.25

I have to select randomly some values from a matrix at the sender side and also select the same random numbers from the same matrix at the receiver side also,how can it be done.can you please give the code.

Any idea how i generate integer random numbers normally distributed?

thaks in advance

want to allcate secondary user to the primary user according to vacant spectrum? please reply me the code

i have a matrix of 10 * 24 .. i want to randomly distribute the elements of matrix so that sum of numbers in each coloum and in each row is same to the original matrix sum .. help me !!

i have a matrix of 10 * 24 .. i want to randomly distribute the elements of matrix so that sum of numbers in each coloum and in each row is same to the original matrix sum .. help me !!

Thanks for the great example on how to use the randperm function.

However, how would you go about the issue of generating 100 consecutive random permutations using you last example and store each result?