1 / 13

CS005 Introduction to Programming Matlab

CS005 Introduction to Programming Matlab. Eamonn Keogh eamonn@cs.ucr.edu. More on Functions. The rand function returns a random number between 0 and 1 Strictly speaking, the interval is (0,1) Every time you use it, you should expect a different number. EDU >> EamonnRand = rand

hcatherine
Download Presentation

CS005 Introduction to Programming Matlab

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS005 Introduction to Programming Matlab Eamonn Keogh eamonn@cs.ucr.edu

  2. More on Functions The rand function returns a random number between 0 and 1 Strictly speaking, the interval is (0,1) Every time you use it, you should expect a different number. EDU>> EamonnRand = rand EamonnRand = 0.0975 EDU>> EamonnRand = rand EamonnRand = 0.2785

  3. Where is the input parameter? If you don’t use one, matlab assumes you just want a single number We can also explicitly ask matlab for a single random number like this… EDU>> EamonnRand = rand EamonnRand = 0.2856 EDU>> EamonnRand = rand(1) EamonnRand = 0.8003

  4. Suppose we want a random number between 0 and 10 instead? We don’t need new function for this. We can just multiple rand by 10 If you want a random number between 0 and 77, just multiple rand by 77 etc. EDU>> EamonnRand = rand(1)*10 EamonnRand = 1.4189 EDU>> EamonnRand = rand(1)*10 EamonnRand = 4.2176 EDU>> EamonnRand = rand(1)*10 EamonnRand = 9.1574

  5. Suppose we want a random number between 90 and 100 instead? We don’t need new function for this. We can just multiple rand by 10, then add 90 If you want a random number between A and B, just use A + (B-A)*rand EDU>> EamonnRand = (rand(1)*10) + 90 EamonnRand = 90.3571 EDU>> EamonnRand = (rand(1)*10) + 90 EamonnRand = 98.4913

  6. Suppose we want a random integer instead For example we want an electronic dice that give use a 1,2,3,4,5 or 6 First lets get a real number between 0 and 6 A + (B-A)*rand Now we want to snap the numbers to the nearest integer (almost true) EDU>> 0 + (6-0)*rand ans = 4.5032 0 1 2 3 4 5 6 One die, two dice

  7. EDU>> round(4.5032) ans = 5 EDU>> floor(4.5032) ans = 4 EDU>> ceil(4.5032) ans = 5 Matlab has three useful built-in functions that might help. round returns the nearest integer to the input argument floor returns the nearest integer that is less than or equal to the input argument ceil returns the nearest integer that is larger than or equal to the input argument 4 5

  8. EDU>> ceil(0 + (6-0)*rand) ans = 4 EDU>> ceil(0 + (6-0)*rand) ans = 3 EDU>> ceil(0 + (6-0)*rand) ans = 1 EDU>> ceil(0 + (6-0)*rand) Putting it altogether, we have our dice toss command. 4 5

  9. EDU>> ceil(1 + (12-1)*rand) ans = 3 EDU>> ceil(1 + (12-1)*rand) ans = 12 EDU>> ceil(1 + (12-1)*rand) ans = 2 Many games are based on the sum you get when you toss two dice. The range is 2 (snake eyes) to 12 (boxcars) So this code gives an integer between 2 and 12 Wrong!

  10. EDU>> ceil(1 + (12-1)*rand) ans = 3 This code gives equal probability to all numbers from 2 to 12. However, the probabilities should differ

  11. EDU>> Dice1= ceil(0 + (6-0)*rand); EDU>> Dice2= ceil(0 + (6-0)*rand); EDU>> Sum_from_two_dice_throws = Dice1 + Dice2 Sum_from_two_dice_throws = 10 Our code should really “throw” two dice, like this..

  12. I ran both methods 100,000 times, and plotted the distribution of numbers.

More Related