110 likes | 125 Views
Learn how random numbers enhance animations, generating motion effects for objects using built-in Alice features. From generating random distances for sliding penguin to creating random motion for swimming goldfish. Dive deep into practical examples and built-in questioning to control motion behavior dynamically.
E N D
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
Built-in questions • Alice provides built-in questions for generating random numbers.
Example • To have the penguin slide forward a random distance, we can write: • The random number question returns a fractional value between 0 and 1.
Demo: A range of values • You can specify a different range of values by specifying a minimumand maximum value. • In this example, the random number will be a fractional value greater than 0 and less than 5.
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.
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.
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:
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
randomMotion • The minimum distance of the move forward instruction is 0 (the goldfish always moves forward).
Demo • To call the randomMotion method, the min and max values must be specified.