Generate random numbers

Keyboard Shortcuts

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 courseI showed you how to create ranges or sequences of numbersthat followed a pattern.Many applications rely on random numbers though.So in this movie I willshow you how to generate random numbersusing a variety of techniques.I have started MATLABand I am working in a blank command window.The most straightforward way to create a random numberis to use the rand function.So at the command window and in the command promptif I just type rand and Enterthen I get a random number.

If I type rand again then I get a different random number.These numbers are only displayedto four decimal places, but the actual numbersare in fact much longer.Another thing to point out is that random numbersin MATLAB are not actually random.They are generated by an algorithmthat is based on a start or a seed valueand you can change the seed value either byspecifying it yourself, by shuffling, which causesthe computer to go through a process of choosing a new seedor you can change it to the default,which puts it back in its normal patternbased on the system clock.

So let me show you what that looks like.If you want to set the random number generatorto a specific seed numberso that you know what the results are going to beyou type rng followed by a left parenthesisand 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 sameseed that I had before.So rng left parenthesis 256right parenthesis then Enter.Again, nothing changes visiblyand then I'll type rand and Enterand I get 0.0458 back.So when I know where we are in the sequencethen I know the next pseudo random numberthat is coming up.If you want to shuffle you can just type rngboth parenthesis and then a single quote,because you're entering in a string,shufflesingle quote to close out the stringand then Enter.

Again, nothing seems to change, do randand you get a different starting value.You can also use default, which againworks off the system clockand I'll type that in, rngleft parenthesis and then in single quotesdefault followed by right parenthesis, Enterand then rand and you see that we geta different random value.Now let's suppose that you want to geta random value that hasa decimal component as opposed to an integer.

To get a random real numberbetween zero and 100you 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 somethinglike round these values either up or downto get rid of the decimal componentyou're not getting a true distribution of randominteger values, that is whole numbers.

Instead you can use the randi functionto get randomly distributed integers from one to 100.So it's a uniformed distribution.So I'll type randi followed bya left parenthesis and then aleft square bracketthen the start of the range, so I have one comma100 followed by a right square bracketto close out the range definition,a right parenthesis then Enterand I get 13.If I were to do the same, R-A-N-D-Ileft parenthesis, start the argument listleft square bracket one comma 100right square bracket right parenthesis, Enterand I get 92 and I can continue doing that.

You can also generate valueswithin the normal distributionand that is the classic bell curvewhere values toward the middle of distributiontend to occur more often than valuestoward the tails of the curve.If you want normally distributed random valuesfrom zero to onethen you can use the randn function.So just type randn and Enterand you get 0.3188,randn again and then you get- 1.3077and again this is a normal distribution or moreappropriately the standard normal distributionso it has a mean or average of zeroand a standard deviation of one.

So about 68% of all values will be withinone standard deviation plus or minus or minus one to oneand then values get more rare as you go out.So I'll just type randn againor a cluster within one standard deviationon the minus side, randn one more timeand we're a little bit above itand again those values are being createdor generated by the internal algorithms of the program.As you can see you have many different waysto generate random numbers ofdifferent characteristics within MATLAB.

Resume Transcript Auto-Scroll

Author

Released

8/28/2018

Learn how to analyze data using one of the most powerful statistical software packages available: MATLAB 2018. MATLAB is a highly useful tool for complex computation as it allows high-order calculations and analysis in matrices. In fact, it treats all of its data as vectors and matrices, so the course starts with a basic linear algebra tutorial outside of the program before describing essential analytical skills performed using MATLAB. Instructor Curt Frye covers creating MATLAB scripts, accepting user input, extending MATLAB with user-defined functions, controlling flow with conditional logic and loops, managing text strings and other data structures like numbers, performing advanced plotting, and summarizing data using statistical analysis functions. By the end of the course, data scientists such as engineers and economist should be able to effectively use MATLAB for numerical analysis, data modeling, and data visualization.