1 / 9

Random numbers

Random numbers . Generating Random Numbers. Generating random numbers rand() is another one of those versatile functions x=rand; x=rand(); %some keep the () to remind themselves it is a function-call vs. a variable name. x=rand(1); %avoid, it’s overdoing it…

kevina
Télécharger la présentation

Random numbers

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. Random numbers

  2. Generating Random Numbers • Generating random numbers • rand() is another one of those versatile functions x=rand; x=rand(); %some keep the () to remind themselves it is a function-call vs. a variable name. x=rand(1); %avoid, it’s overdoing it… x=rand(2); %a 2-rows by 2-columns matrix x=rand(2,5); %a 2-rows by 5-columns matrix 

  3. rand() and a little bit of algebra: +- • What happens to a number k between 0 and 1 if it is added to another number? For example: What can we say about: 2+k ? What can we say about: k-4 ? >> The interval shifts left/right. k 1 0 k 0 2 1 3

  4. rand() and a little bit of algebra: */ • What happens to a number k between 0 and 1 if it is multiplied by another number? For example: What can we say about: 5*k ? What can we say about: k/2 ? >> The interval grows/shrinks. k 0 1 k 0 5

  5. End of algebra • So.. Using a combination of arithmetic operators, how would you generate these values (both excluded): k1 = rand_______________________; k2 = rand_______________________; k1 k2 15 -5.5 20 5.5

  6. Conclusion • To generate 1 float in the interval : ]a,b[ k = rand*(b-a)+a; This is not a formula worth remembering.. Just remember algebra!

  7. Example1 Thermometer A new thermometer is being built. In addition to showing the temperature, the manufacturer also wants to indicate a warning if the temperature is above or equal to 104 degrees Fahrenheit. You are being paid ($$$) to develop the program that will eventually be within the thermometer. It’s been a year, and still no thermometer.. How long are you going to wait???

  8. Example2 Rockets 0/1’s? • How about rockets??? How does software get written? Do we waste a rocket each time? • During launch, so many sensors give back information!!! • A couple of them are…. • Doors locked? True/False • Oxygen flowing properly? True/False • Fuel being delivered to engine? True/False

  9. Wrapping up • Vocabulary • Function call • Arguments • Collecting • Return-values • Versatile • New notions • Generating random numbers • Generating 1 random float value • Manipulating it to desire random range wanted • Generating a zero/one to simulate false/true • Examples • Temperature: rand() • Rocket: rand() and ceil()!!

More Related