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

Slides:



Advertisements
Similar presentations
AliceWhileLoop1 While Loop in Alice Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009.
Advertisements

 Variables  What are they?  Declaring and initializing variables  Common uses for variables  Variables you get “for free” in Processing ▪ Aka: Built-in.
CS320n –Visual Programming Class-level Methods and Inheritance – Part 1 Mike Scott (Slides 4-3-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger.
Parameters and World-level methods Alice. Our Dragon world The dragon must to take off and fly, to carry the princess.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Repetition Structures
While: Indefinite Loops Alice. Repetition In some situations, we don’t know exactly how many times a block of instructions should be repeated. All we.
While: Indefinite Loops Sec 8-14 Web Design. Objectives The student will: Understand what an Indefinite Loop is Understand how create an indefinite loop.
Repetition: Definite Loops Alice. Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example:
Introducing While loops (and random numbers too) Alice.
CS320n –Visual Programming Indefinite Loops (Slides 7-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
While: Indefinite Loops Alice. Repetition In some situations, we don’t know exactly how many times a block of instructions should be repeated. All we.
Alice in Action with Java Chapter 5 Random Numbers.
Tips & Techniques 6 Random Numbers and Random Motion Alice.
CS320n –Visual Programming Functions Mike Scott (Slides 6-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
CS320n –Visual Programming Interactive Programs Mike Scott (Slides 5-1)
Functions and Conditionals in Alice 1 Stephen Cooper Wanda Dann Barb Ericson September 2009.
Shorter of two objects and changing color Functions, events and setting the color Susan Rodger, Duke University June 2008.
Any questions on today’s homework? (Sections 1.6/1.7) Reminder: You should be doing this homework without using a calculator, because calculators can’t.
Working with Numbers in Alice - Converting to integers and to strings - Rounding numbers. - Truncating Numbers Samantha Huerta under the direction of Professor.
by Chris Brown under Prof. Susan Rodger Duke University June 2012
CompSci 4 Chap 5 Sec 1 Oct 13, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas.
Study Guide For Test Chapter 5, 6,& 7 Test is Friday, May 15th.
Shorter of two objects and changing color V2 Functions, events and setting the color in sequence and randomly This is a modification of the Changing Color.
Modules. A module is a file containing Python definitions and statements intended for use in other Python programs. There are many modules as part of.
Events Chapter 7 Part 2. While a Key is Pressed Event Specialized event An event occurs when you press a key and continues until you take your finger.
CompSci 4 Chap 7 Sec 2 Apr 7, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas.
Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed.
Comparing and ordering rational numbers
CompSci 4 Chap 8 Sec 1 Nov 17, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas.
Variables and Inheritance A More Complex Example Alice.
Writing Our Own Functions Alice. Functionality A function receives value(s), performs some computation on the value(s), and returns (sends back) a value.
What we will do today Learn about functions in Alice.
Functions Alice.
Variables and Inheritance Part 1
CS320n –Visual Programming Introduction to Recursion (Slides 8-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
CS320n – Elements of Visual Programming Lists Mike Scott (Slides 9-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
CS320n –Visual Programming Advanced Recursion (Slides 8-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
CS320n – Elements of Visual Programming Sending Parameters to Event Handler Methods (Slides 5-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Methods and Data Programming with Alice and Java First Edition.
Repetition: Definite Loops Alice. Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example:
Variables and Functions Alice. Naming is Important If you get a new pet one of the first things you do is name it Gives you a way to refer to the new.
Functions Sec 8-11 Web Design. Objectives The Student will: Understand what a function is Know the difference between a method and a function Be able.
Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Alice Workshop Functions, Loops, and Parameters.
Today’s Agenda 1.Collect Pre-Lab 3 2.Alice Programming Assignment 3.Built-in Functions 4.Expressions 5.Control Structure 6.Assign pair programming teams.
Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Alice Workshop Variables & Conditions.
ForLoopsInAlice1 Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009 Counted (For) Loops in Alice.
Questions Alice. Functionality A question receives value(s), performs some computation on the value(s), and returns (sends back) a value.
Georgia Institute of Technology More on Creating Classes part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
CS320n –Visual Programming Definite / Counted Loops (Slides 7-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
Repetition: Definite Loops Sec 53 Web Design. Objectives The Student will: Understand loops and why they are used Understand definitive loops Know how.
CompSci 4 Chap 6 Sec 2 Sep 30, 2010 Prof. Susan Rodger “All your troubles are due to those ‘ifs’,” declared the Wizard. If you were not a Flutterbudget.
Repetition: Definite Loops Alice. Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example:
CS320n –Visual Programming Problem Solving Case Study (Slides 6-3) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
CompSci 4 Chap 10 Nov 22, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas.
Tips & Techniques 6 Random Numbers and Random Motion Alice.
CS320n –Visual Programming Execution Control with If / Else and Boolean Functions (Slides 6-2-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for.
Variables and Inheritance Part 1 Alice. Review: Properties A class defines properties for its own kind of object. When an object is created (instantiated),
CS320n – Elements of Visual Programming List Search Mike Scott (Slides 9-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
Learn about functions in Alice
While: Indefinite Loops
Learning Java with Alice 3.0 Game Design Kathy Bierscheid
CS320n –Visual Programming
Alice and Media Computation August 2009
Repetition: Definite Loops
Parameters and World-level methods
Counted (For) Loops in Alice
Repetition: Definite Loops
Repetition: Definite Loops
Presentation transcript:

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

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

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

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

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

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

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

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

Visual ProgrammingRandom Numbers and Random Motion 9 Improved Random Movement

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.

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

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

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

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

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

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.

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:

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

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

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