cs320n –visual programming random numbers and random motion (slides 6-3) thanks to wanda dann,...

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

Upload: juliana-robinson

Post on 19-Dec-2015

213 views

Category:

Documents


1 download

TRANSCRIPT

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

CS320n –Visual Programming

Random Numbers and Random Motion

(Slides 6-3)

Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.

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

Visual Programming Random Numbers and Random Motion

2

What We Will Do Today• Look at the use of random numbers in

programs

• Start the material on repetition

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

Visual Programming Random 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

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

Visual Programming Random Numbers and Random Motion

4

Built-in Functions• Alice provides built-in functions for

generating random numbers.

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

Visual Programming Random Numbers and Random Motion

5

Example• from assignment 4• The knight has a class level method

• replaced distance and revolutions with random number function

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

Visual Programming Random 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

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

Visual Programming Random Numbers and Random Motion

7

What will knight’s behavior be?

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

Visual Programming Random 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

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

Visual Programming Random Numbers and Random Motion

9

Improved Random Movement

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

Visual Programming Random 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.

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

Visual Programming Random Numbers and Random Motion

11

Random Hopping• Rabbit hops (moves up and then down) a

random amount

• Predictions on behavior?

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

Visual Programming Random 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

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

Visual Programming Random 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

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

Visual Programming Random 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

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

Visual Programming Random Numbers and Random Motion

15

Using Variables• Demo of the bunny hop

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

Visual Programming Random 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.

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

Visual Programming Random 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:

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

Visual Programming Random 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, maxDo together fish move up a random number distance fish move left a random number distance fish move forward a random number distance

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

Visual Programming Random Numbers and Random Motion

19

randomMotion

• The minimum distance of the move forward instruction is 0 (the goldfish always moves forward).

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

Visual Programming Random Numbers and Random Motion

20

Demo• To call the randomMotion method, the min and

max values must be specified.