CS320n –Visual Programming Random Numbers and Random Motion (Slides 6-3) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas. What We Will Do Today • Look at the use of random numbers in programs • Start the material on repetition Visual Programming Random Numbers and Random Motion 2 Random Numbers • Random numbers are used in certain kinds of computer programs • Examples: – – – – security for web applications encryption for satellite transmissions gaming programs scientific simulations • In this session, we will look at examples of how to use random numbers in animations Visual Programming Random Numbers and Random Motion 3 Built-in Functions • Alice provides built-in functions for generating random numbers. Visual Programming Random Numbers and Random Motion 4 Example • from assignment 4 • The knight has a class level method • replaced distance and revolutions with random number function Visual Programming Random Numbers and Random Motion 5 The random number function • World’s random number function • returns a fractional value between 0 and 1 • uniformly distributed – each number has an equal chance of being picked • Side track: generation of random numbers is a whole area of study in computer science Visual Programming Random Numbers and Random Motion 6 What will knight’s behavior be? Visual Programming Random Numbers and Random Motion 7 Picking from Ranges of Values • Default random numbers are between 0 and 1 • Can alter the min and max value • Currently knight always turns left – how can this be changed? • Alter the range for the distance moved forward Visual Programming Random Numbers and Random Motion 8 Improved Random Movement Visual Programming Random Numbers and Random Motion 9 Demo: Integers (whole numbers) • To generate a random integer value (having no decimal point or digits to the right of a decimal point), select integerOnly from the more… options and make it true. Visual Programming Random Numbers and Random Motion 10 Random Hopping • Rabbit hops (moves up and then down) a random amount • Predictions on behavior? Visual Programming Random Numbers and Random Motion 11 Remembering Things • Parameters were used to pass information into a method or function • The parameter could take many different values – who -> wizard, dragon, troll, princess – target -> any object in the world • The parameter can be used over and over in the method Visual Programming Random Numbers and Random Motion 12 Variables – in a Method • A variable in a method – – – – stores a value has an initial value can have its value changed can only be used in the method it is declared in • To create a variable • To use the value in a variable – drag the variable into place Visual Programming Random Numbers and Random Motion 13 Setting Variable to other Value • To give a variable a numeric value can assign it a value at top of method • to give it the result of a function call drag variable into method and choose set value • pick dummy value and then replace with random Visual Programming Random Numbers and Random Motion 14 Using Variables • Demo of the bunny hop Visual Programming Random Numbers and Random Motion 15 Random Motion • In some animations, we want an object to move to a random location. • We call this random motion. • For example, the goldfish in this world is to swim in a random motion. Visual Programming Random Numbers and Random Motion 16 Six possible directions • Of course, six move directions are possible – forward, backward, left, right, up, down • We can eliminate backward because goldfish do not swim backward. • To simplify the code, we can take advantage of negative numbers. – For example, this instruction actually moves the goldfish right: Visual Programming Random Numbers and Random Motion 17 Storyboard • Only three move instructions are needed: – – – up (will move down if number is negative) left (will move right if number is negative) forward (no backward motion) • Two parameters (min, max) will be used to restrict the motion of the fish to a nearby location-- to look like swimming. fish.randomMotion: Parameters: min, max Do together fish move up a random number distance fish move left a random number distance fish move forward a random number distance Visual Programming Random Numbers and Random Motion 18 randomMotion • The minimum distance of the move forward instruction is 0 (the goldfish always moves forward). Visual Programming Random Numbers and Random Motion 19 Demo • To call the randomMotion method, the min and max values must be specified. Visual Programming Random Numbers and Random Motion 20