Elsewhere in this course, you learned how to create ranges, or sequences, of numbers that followed a pattern. Many applications rely on random numbers. In this video, discover how to generate random numbers using a variety of techniques.
- [Instructor] Elsewhere in this course I showed you how to create ranges or sequences of numbers that followed a pattern. Many applications rely on random numbers though. So in this movie I will show you how to generate random numbers using a variety of techniques. I have started MATLAB and I am working in a blank command window. The most straightforward way to create a random number is to use the rand function. So at the command window and in the command prompt if I just type rand and Enter then I get a random number.
If I type rand again then I get a different random number. These numbers are only displayed to four decimal places, but the actual numbers are in fact much longer. Another thing to point out is that random numbers in MATLAB are not actually random. They are generated by an algorithm that is based on a start or a seed value and you can change the seed value either by specifying it yourself, by shuffling, which causes the computer to go through a process of choosing a new seed or you can change it to the default, which puts it back in its normal pattern based on the system clock.
So let me show you what that looks like. If you want to set the random number generator to a specific seed number so that you know what the results are going to be you type rng followed by a left parenthesis and then whatever you want the seed to be. So let's type in 256 and Enter. Okay, it was only an internal change. So type rand and you get 0.0458. If I type rand again then I get a different value.
Now let's see what happens if I put in the same seed that I had before. So rng left parenthesis 256 right parenthesis then Enter. Again, nothing changes visibly and then I'll type rand and Enter and I get 0.0458 back. So when I know where we are in the sequence then I know the next pseudo random number that is coming up. If you want to shuffle you can just type rng both parenthesis and then a single quote, because you're entering in a string, shuffle single quote to close out the string and then Enter.
Again, nothing seems to change, do rand and you get a different starting value. You can also use default, which again works off the system clock and I'll type that in, rng left parenthesis and then in single quotes default followed by right parenthesis, Enter and then rand and you see that we get a different random value. Now let's suppose that you want to get a random value that has a decimal component as opposed to an integer.
To get a random real number between zero and 100 you can multiply the number by 100. So you type rand then an asterisk and 100, Enter and you get 90.5792. So the base value was .905792. One thing to note is that if you were to do something like round these values either up or down to get rid of the decimal component you're not getting a true distribution of random integer values, that is whole numbers.
Instead you can use the randi function to get randomly distributed integers from one to 100. So it's a uniformed distribution. So I'll type randi followed by a left parenthesis and then a left square bracket then the start of the range, so I have one comma 100 followed by a right square bracket to close out the range definition, a right parenthesis then Enter and I get 13. If I were to do the same, R-A-N-D-I left parenthesis, start the argument list left square bracket one comma 100 right square bracket right parenthesis, Enter and I get 92 and I can continue doing that.
You can also generate values within the normal distribution and that is the classic bell curve where values toward the middle of distribution tend to occur more often than values toward the tails of the curve. If you want normally distributed random values from zero to one then you can use the randn function. So just type randn and Enter and you get 0.3188, randn again and then you get - 1.3077 and again this is a normal distribution or more appropriately the standard normal distribution so it has a mean or average of zero and a standard deviation of one.
So about 68% of all values will be within one standard deviation plus or minus or minus one to one and then values get more rare as you go out. So I'll just type randn again or a cluster within one standard deviation on the minus side, randn one more time and we're a little bit above it and again those values are being created or generated by the internal algorithms of the program. As you can see you have many different ways to generate random numbers of different characteristics within MATLAB.
- Defining variables and contains
- Exploring operators
- Summarizing with built-in functions
- Generating random numbers
- Defining vectors and matrices
- Accepting input in scripts
- Writing and reading data from external files
- Creating custom functions
- Using conditional logic
- Repeating operations with loops
- Working with text strings
- Plotting data and function output
- Formatting, saving, and printing plots
- Using statistical and matrix functions