22
15-101 Variables & Random Number Generation

15-101 Variables & Random Number Generation. A penguin is playing arctic basketball. The penguin has a basketball and will push the basketball toward

Embed Size (px)

Citation preview

15-101Variables &

Random Number Generation

A penguin is playing arctic basketball. The penguin has a basketball and will push the basketball toward a hole in the ice. Each push causes the basketball to move forward a random distance.

If the penguin gets the basketball in the water, the penguin scores two points.

Recall: Problem

Problem Solving step 2 Design an algorithm and set up scene

Do in order penguin swagger penguin push basketball a random distance If basketball is in the water, score 2 points

Implementation – translate algorithm into code (note that the basketball is now the target)

Problem Solving step 3

Do in order penguin swagger penguin push basketball a random distance If basketball is in the water, score 2 points

This italicized item will be modified / added in future presentations

Need to modify

At runtime, a program is allocated a stack of memory (bytes) in which to store its data.– Several data items may be stored in the

stack

Runtime stack

Data items

A variable is a name for a location in memory where an item of data is stored

Each data item in the stack is given a unique name (an identifier)• Example: in diagram at right, done is a name for location 4

We say a variable “represents a specific value” The value stored in a variable can and will change

when a program runs Thus -> variable

Variable

done

The data item stored at a specific location has a specific data type, which describes the characteristics of the stored information

Range of values Legal operations

Data types used in Alice 3

Declaring a variable Drag the local tile into the code editor

A variable is declared by specifying– Type– Name– Initial value

Declaring a variable

Double randDistance = 0.0;

Alice Java

Assignment: Stores a value in the variable's memory space

• At the time a variable is declared, it’s data type determines how many bytes of memory the variable is allocated.

• Example:

Byte Allocation

randDistance has 8 bytes of memory where a real number may be stored

The previously written pushObject procedure uses the distance parameter to move the target object forward 2.75 meters

but we want the target (basketball) to move forward a random distance...so we need to send a random number to the distance parameter.

Value sent to parameter

In MyScene’s run method, Add a local variable, randDistance, that will represent a random distance.

Replace default initialization value (0.0) with a random value in the range of 0.5 to 5.0 (Why?)

Random distance for the ball

replace 0.0 with one of the Random number functions

Random functions

A function designed to generate a random sequence of numbers or symbols that lack any pattern, i.e. appear random - Wikipedia

Random number functions

The next value in the random sequence will be in the range

0.0 <= value < 1.0

The next value in the random sequence will be in the range

first ??? <= value < second ???

Which function is better for our purposes?

Build the random number expression Build the random expression from the cascading menus.

Resulting statement:

Use the randDistance variable We have generated a random value in the range

0.5 >= value < 5.0, and stored it in our local variable, randDistance

We now need to use this variable as the argument to the pushObject method for the distance parameter

Resulting statement:

click

select

Problem Solving Step 4: Testing

Because we are using random numbers, we need to run this code many times to convince ourselves that successive runs yield random results.

Alice provides visual feedback Another common technique for testing is to display a value as a textual string

Problem Solving Step 4: Testing

Because we are using random numbers, we need to run this code many times to convince ourselves that successive runs yield random results.

Alice provides visual feedback Another common technique for testing is to display a value as a textual string

We could use say or think to display the value in Alice.

Example, using say

Displaying a value: Alice

The think and say procedural methods expect a String argument To create a String, we can use concatenation– Concatenate means: to append one String

to another String– Symbol for concatenation is '+'

Example: “hello” + “ world” → “hello world”

Example: Alice Drag in say statement tile -> Select “hello” to begin Replace “hello”

Select concatenate symbol tile Select operands

Custom String

One of the operands is a custom string, so Alice displays a dialog box where a string can be entered

Resulting statement