Updated March 22, 2023

## What is a Random Number Generator?

Before understanding the Random Number Generator in Matlab let us first study what is Random number Generator. Random Number Generator is the creation of random numbers without any decision or noticeable patterns among them. There are various ways of generating random numbers in MATLAB with different applications. It is used in many programming languages for the generation of random values within the specified range. There are different functions that are used as per the language. They are mainly used in the computer science field, research, and statistical related work.

### Random Number Generator in Matlab

In MATLAB, pseudo-random numbers are generated using various functions like rand, randi, and randn. Each function serves a different purpose in MATLAB as listed below:

**rand:**This function is used to generate uniformly distributed random values.**randi:**This function is used to generate normally distributed pseudo-random values.**randn:**This function is used to generate normally distributed random values.**randperm:**This is used to create permuted random values.**rng:**This controls the random number generation**RandStream:**This is used for the stream of random numbers.

rand,randn,randi, and randperm are mainly used to create arrays of random values.

### Functions in Random Number Generator in Matlab

Below are the function serves a different purpose in MATLAB as listed below:

#### 1. rand

rand function is used when the distribution is uniform and always generate real numbers between 0 and 1. It is denoted by function rand().

**Example:** `a=rand(100,1)`

The above example explains that a is a 100 by 1 column vector which contains numbers from a uniform distribution. contains the values between 0 and 1. The graph of this is normally flat since it is drawn from a uniform distribution.

rand(‘state’) returns the current state of the generator. We can also change the state of the generator using the below code:

**rand(‘state’,s):**It resets to the state s.**rand(‘state’,0):**It sets the generator to its initial state.**rand(‘state’,k):**It sets the generator to its kth state, for any value of k.**rand(‘state’, sum(100*clock)):**It resets to a different state each and every time.

#### 2. randi

This function returns double integers which are drawn from the distribution which is discrete and uniform. It is denoted using randi()

**Example :** `b= randi(1,1000,100)`

Here b contains the integers drawn from a uniform distribution in the range from 1 to 100. The graph of the resultant set will be generally flat since it returns the numbers from the uniform distribution.

#### 3. randn

This function returns integers resulting from the normal distribution. It is noted using function randn(). The graph of the resultant set follows a normal distribution having mean 0 and standard deviation 1.

**Example:** `c=randn(100,1)`

randn(‘state’) returns the current state of the generator. We can also change the state of the generator using the below code:

- randn(‘state’,s): It resets to the state s
- randn(‘state’,0): It sets the generator to its initial state
- randn(‘state’,k): It sets the generator to its kth state, for any value of k.
- randn(‘state’, sum(100*clock)): It resets to a different state each and every time.

#### 4. randperm

This function returns the array of unique values. The main difference between randi and randperm is that randi contains an array of values that can be repeated but randperm contains an array of integers that are unique. It is denoted using randperm().

**Example:** `d= randperm(20,10)`

This is a 1 by 10 array which contains integers in the range [1,20].

### Functions Generation of Random Numbers

There are also various functions used to control the generation of random numbers. Please find the below for your reference:

**rng (seed)**: It seeds the generation of random numbers so that it draws the random numbers that are predictable.**rng (shuffle):**This generates random numbers depending on the current time. So, it generates the numbers after calling the rng function.**rng (‘default’):**This function is used to sets the settings used by the rand, randn,randi function to their default state.**scurr:**It returns the settings used in rand, randn, randi function currently.**rng(s):**It restores the setting used for creating random numbers in rand, randn,randi function.

### Conclusion – Random Number Generator in Matlab

Random Number Generation has many applications in real life in a very practical way. They are mainly used for authentication or security purposes. Various slot machines, meteorology, and research analysis follow a random number generator approach to generate outcomes of various experiments. So, knowing the background of the generation of random numbers practically is important to understand the applications of it in a better way.

### Recommended Articles

This is a guide to Random Number Generator in Matlab. Here we discuss Various functions in Random Number Generator in Matlab in detail. You can also go through our other related articles to learn more –