Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS320n –Visual Programming Random Numbers and Random Motion (Slides 6-3) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

Similar presentations


Presentation on theme: "CS320n –Visual Programming Random Numbers and Random Motion (Slides 6-3) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas."— Presentation transcript:

1 CS320n –Visual Programming Random Numbers and Random Motion (Slides 6-3) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

2 Visual ProgrammingRandom Numbers and Random Motion 2 What We Will Do Today Look at the use of random numbers in programs Start the material on repetition

3 Visual ProgrammingRandom Numbers and Random Motion 3 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

4 Visual ProgrammingRandom Numbers and Random Motion 4 Built-in Functions Alice provides built-in functions for generating random numbers.

5 Visual ProgrammingRandom Numbers and Random Motion 5 Example from assignment 4 The knight has a class level method replaced distance and revolutions with random number function

6 Visual ProgrammingRandom Numbers and Random Motion 6 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

7 Visual ProgrammingRandom Numbers and Random Motion 7 What will knight’s behavior be?

8 Visual ProgrammingRandom Numbers and Random Motion 8 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

9 Visual ProgrammingRandom Numbers and Random Motion 9 Improved Random Movement

10 Visual ProgrammingRandom Numbers and Random Motion 10 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.

11 Visual ProgrammingRandom Numbers and Random Motion 11 Random Hopping Rabbit hops (moves up and then down) a random amount Predictions on behavior?

12 Visual ProgrammingRandom Numbers and Random Motion 12 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

13 Visual ProgrammingRandom Numbers and Random Motion 13 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

14 Visual ProgrammingRandom Numbers and Random Motion 14 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

15 Visual ProgrammingRandom Numbers and Random Motion 15 Using Variables Demo of the bunny hop

16 Visual ProgrammingRandom Numbers and Random Motion 16 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.

17 Visual ProgrammingRandom Numbers and Random Motion 17 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:

18 Visual ProgrammingRandom Numbers and Random Motion 18 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

19 Visual ProgrammingRandom Numbers and Random Motion 19 randomMotion The minimum distance of the move forward instruction is 0 (the goldfish always moves forward).

20 Visual ProgrammingRandom Numbers and Random Motion 20 Demo To call the randomMotion method, the min and max values must be specified.


Download ppt "CS320n –Visual Programming Random Numbers and Random Motion (Slides 6-3) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas."

Similar presentations


Ads by Google