In this tutorial, we will learn how to use Rand and Randi Matlab Operator. But before moving towards these commands, lets first have an idea about random function generator. As the name implies, random function generator is a function or an algorithm which creates a sequence of random numbers. They have numerous applications such as casinos and roulette wheels use random number generators. Probabilistic algorithms work on random numbers. Some other uses of random number generators are in cryptography, computer simulation mathematically modelling etc. Matlab provides different operators and functions to generate random numbers. Two of them are Rand() and Randi() function. Lets discuss them one by one.
Rand function
A Rand Function generates a uniformly distributed random number between 0 and 1. If you want to generate a single uniformly distributed number in a range of 0 to 1, then you can do this by using rand function as illustrated in the snippet below. It doesn’t take any argument and returns a random number. Whenever a worksheet is modified or reopened, the function recalculates and generate a new random number every time.
Example:

Similarly, we can also get a matrix of random numbers between 0 and 1 using this operator. For this, we need to pass an argument defining the size of a matrix to the function. The syntax is:
y=rand(n) % generate a nxn matrix of random numbers
y=rand(n,m) % generate a nxm matrix of random numbers
Example:


So this is all about Random Number Generator. You can Generate any random number or matrix with values between 0 and 1 using this operator.
Randi Matlab
In this paragraph, we will study different uses, syntax, and examples of Random Integer Operator. You might be wondering what makes Randi different from Rand. Randi allows you to choose your maximum value and returns you a random integer between that value. Doesn’t it sound interesting? Let’s see a practical example of how we can use it. Let’s set the maximum value of 10 and see what random integers does it generate.

It gave us an output of 2. We can see that it is generated randomly. Let’s look at more examples and find out how can we make a matrix of random integers using this operator.
randi (maximum number, n, m)
The first argument is indicating the maximum limit whereas n and m are indicating the size of the matrix i.e rows and columns. Therefore, if we want a randomly generated matrix of 2*2 with all the integers less than 5, we would write it like:
randi (5, 2, 2)

Now if you want to store the output, you can simply write
r*randi(x, y, z)
This command will store your matrix of y columns, z rows, and x maximum into r.
Matlab is the most popular programming language. Rand() and Randi() are often misunderstood, but these are two different terms. In this tutorial, we have learned the different ways to obtain the size and length of lists. If you want to learn about Matlab, please follow our website. Let us know if you have queries regarding this topic or want us to cover a certain topic. It would be highly appreciated.
For any questions related to Matlab feel free to Contact Us.