[MUSIC] So far, we've always created matrices manually like this. There's a two by four matrix, but MATLAB has a few functions that let you create matrices of arbitrary size really quickly. Here's one. You can see what it gives you, zeros. A matrix of all zeros and this is a 5 by 6 matrix. What if you want ones? You can get those too. What if you want all fives? Well. That's the way you get that. Oh and let's look at this. Just give it one argument. Oh, four-by-four. This is just another example of polymorphism. If you give it two arguments, the first one's the number of rows, the next one's the number of columns. If you give him one argument you're giving the rows and the columns. If you want a vector, let's do ones. This gives you a row vector. If you want a column vector, we'll do it with zeros. And if you want a diagonal matrix you can have that. Suppose you want a matrix of 7391 on the diagonal. 791, there you are, a diagonal matrix course as elements that are non zero only where the row index equals a column index so here's one, one. This is two, two, three, three, four, four. We wanna look at random numbers now random numbers are very important in many fields such as signal processing and on and on. And MATLAB has some different functions to support this. We've seen rand in the last lesson but let's revisit that one now. You remember rand? There. It outputs numbers between zero and one that are uniformly distributed on that range, meaning that any number in the range zero to one has the same probability of being returned by rand at any given time. And as we've seen, it can return matrices. This is one we did a lot of in a function called myRand. And like these other matrix producers, when you give rand one input argument it treats that as the number of rows and columns and you get a square matrix. In our previous lesson we showed how to get a range other than zero to one by combining rand/ with arithmetic operators. For example here's how you get a 5 by 4 matrix of numbers between 1 and 11. What if you want random integers in that range? Well first note that the whole number part of these numbers ranges just from 1 to 10. If we could just drop the fractional part, we'd have integers that go from 1 to 10. Well MATLAB provides the function fix that'll do that. It returns the whole number part of a number. In other words fix rounds the number towards zero. Let's just give fx the expression we just came up with. So, I'm gonna hit the up arrow, I'm gonna put parentheses around this. There, that doesn't happen to be a one there that time but these numbers range from one to ten, I'm going to repeat that command and there you see some numbers little bit different there is a one there. Maybe that's reassuring that it's one to ten. But MATLAB will do better than that. It provides a function that returns random integers. It's called randi. randi, and I'm going to give it three arguments, ten. You can see all these hints there. There's lots of different ways to collar, by 4. That 10 means that we want integers that go from 1 to 10. The 5 and the 4 say that we want a 5 by 4 matrix of them, and so there they are. And by now, you've probably correctly guessed that rand is polymorphic. If you give it just two arguments, the first one is still treated as the maximum possible integer, but the second argument is the number of rows and columns of a square matrix. So randi(20,5) will return a five by five matrix of integers between one and ranging from 1 to 20. You can also call randi one other way. Well, more than one other way, but as you can see there, if you give a vector for this first argument of two elements, five say Ten. That means that you want numbers that range from five to ten and now two, three, says that we wanna two by three matrix up. Many times we need random numbers that have a normal distribution Instead of a uniform distribution. This is also known as the Gaussian distribution. Sometimes it's called the Bell curve. Here's how you get that. You use the function rand n. The n's for normal. I give it a five. All the inputs are the same. A five means a five by five matrix. The numbers range from minus infinity to infinity clustered about zero with a standard deviation of one. And a lot of times we want a lot of these numbers. Suppose you want. A million of them. Better not forget that semi-colon. There, didn't take long. Generated a row vector of a million numbers, distributed normally, with a mean of zero and a standard deviation of one. Now let's look at this distribution using using the hist, or histogram function. Figure pops up and we see a nice little plot. You can see this bell curve here. The first argument is the set of numbers, the list of numbers. We've got a million numbers there. And the second argument is the number of bends. So, there's 100 bends divided up. In a reasonable way MATLAB does that. And you can see the number of points, actually the number of random numbers that was generated by rand and. In this little range right here around zero is very large and it drops off as we get out here and they get more rare and more rare. We've seen a number of ways to get random numbers from MATLAB. Now I'm gonna tell you a dirty little secret about these random numbers. They're not random. For example, the first time we call rand, after we start MATLAB, it returns 0.8147. That exact same number every time. Go ahead, try it if you don't believe me. Not all that random, is it? And why is that, you may ask? Well a computer is a deterministic machine. Everything it does is determined by an algorithm. There's no real randomness involved. So you don't get a truly random number, instead random number generator uses some fancy math to generate a pseudo random number sequence. It's pseudo because it's seems to be something that it's not. But the sequence of numbers that it generates sure looks random because there's no discernible way to predict the next number from the ones that came before it. For almost any purpose, unpredictability is all we need and that's what we get. Well, almost. When we start MATLAB, the algorithms initialize the same way every time, so it generates the same sequence every time. Now, that may seem like a bad thing since it always gives us the same numbers. In fact, it's a good thing. It's a feature, not a bug, as the popular saying goes among us programmers. It's a feature because it helps us during the development of our programs. When we're developing a program, we often use the random number generator to give us many thousands of test cases. Just as often, we need to see what happens when we change the program. We use the same input. Well, the only way to do that is to generate the same numbers repeatedly. So, it's very useful that MATLAB generates the same pseudo random sequence each time it starts. Of course, restarting MATLAB every time you need to restart the random number generator hardly seems convenient. So MATLAB provides a separate function that will reinitialize the random number generator for you without restarting MATLAB. It's called RNG, which you might guess stands for random number generator. It takes an input argument and it uses that to determine the sequence that rand will produce when you call it It also uses a neat trick to let you initialize randomly. This trick allows you to get a different sequence every time you run your program, even if you restart MATLAB in the meantime. All this may sound complicated, it's really not. Let's switch to MATLAB and you'll see. Okay to demonstrate this pseudo-randomness idea, I've quit MATLAB and I'm gonna restart and ask it for three random numbers. There, MATLAB's awake. And I'm asking for the three random numbers. And there they are, they look pretty random, I guess. Let me copy those to my clipboard. Well, let's do that again. Let's quit and restart. You know they look pretty familiar. So let's take a look at that clipboard. There, yeah that's the same numbers. Looks like every time MATLAB wakes up from oblivion it's starts all over at the same point sort of like ground hog day that was a great movie by the way so anyway here's how to have a new ground hog day any time you want it without quitting and restarting MATLAB first let's get rid of these numbers we pasted on here. Then I'm gonna call RNG and give it an input of zero. Giving it that zero as input resets the random number stream to the beginning right where MATLAB always start it when it first opens up. So we get the same numbers. You can use any other non negative integer to start at a different place in the pseudo sequence, one for example. There that starts a set a new position. And by the way, RNG sets both rand n for the normal distribution and randi for the integers too. Let's look at those. I'm putting them all in brackets here. So that they'll be on one line, and one vector. And I gave randi one argument, when you do that it gives back one pseudo random number, I should say one pseudo random integer, ranging from one to that argument. And you can start at that same point in the random sequence anytime you want to. We gave rand a 1 twice, so we started at that same position twice and got the same numbers both times. Or you can start at some other point. I went up arrow to get that command back and I'm going over here to change this one to a two. And of course, if I repeat that command, the same random numbers again. You want a more random sequence. An actual random sequence instead of this pseudo-random business. You can input a string. The string shuffle to RNG like this. Let me go back, change my argument over here to the string S-H-U-F-F-L-E got to end that string with another single quote. Let's repeat that see if we get the same thing. No, and let's repeat it again. We're getting different numbers in the sequence even though we're inputting the same argument to RNG every time. Well, how does that work? Well, it uses that trick I mentioned to start the pseudo random sequence number stream at a random point. It does that by getting the current reading of the system clock to generate a truly random number. Since that clock has microsecond resolution, this is all but guaranteeing a random sequence that'll never be repeated. [MUSIC] [NOISE] [APPLAUSE]