Terrapin Resources
Math Fun

RANDOM     PICK     COUNT     NUMBER?     ABS     PI

This section shows you how to use math commands that are available in Logo.


RANDOM number
(RANDOM lowest highest)


RANDOM takes a number as input and reports a random number from 1 through the input number.

RANDOM 10 reports a number from 1 to 10.

(RANDOM 15 20) reports one of these numbers: 15, 16, 17, 18, 19, 20.

» Things to Try

How would you write an instruction to report a number from 0 to 10?

Simulate a coin by flipping it 100 times. Can you write a procedure to keep track of the results?

Simulate rolling a die 99 times. Try to keep track of the results.

Can you use RANDOM to place a dot anywhere on the screen, not just in the upper right quadrant?


PICK word
PICK list
PICK number


PICK takes a word or list as input and randomly reports one of the items in it.

PICK "ABCDEFGHIJKLMNOPQRSTUVWXYZ
Result: Q

PICK [DOG CAT BIRD HAMSTER PONY]
Result: CAT

PICK 0123456789
Result: 3

» Things to Try

Find interesting ways to use PICK.

Could you use it to report results of flipping a coin or rolling a die many times?
It might be more convenient than using RANDOM. What do you think?


COUNT word
COUNT list
COUNT number


COUNT reports the number of items in its input.

If the input is a word, COUNT reports the number of characters.

COUNT "ABCDEFGHIJKLMNOPQRSTVWXYZ Result: 25 ; Is that correct? Look carefully at the input!

If the input is a number, it counts the number of digits. In this case, a number is a word.

COUNT 12345
Result: 5
COUNT "12345
Result: 5

If the input is a list, it reports the number of elements in the list. Note that a list can contain other lists. Consider these examples:

COUNT [SOMEWHERE OVER THE RAINBOW]
Result: 4
COUNT [[ONE MISSISSIPPI] [TWO MISSISSIPPI]]
Result: 2
COUNT [RED ORANGE YELLOW GREEN [MEDIUM BLUE] INDIGO [VIOLET OR PURPLE]]
Result: 7

» Things to Try

Practice counting different types of inputs to COUNT to understand how it works.

Think of how you could use COUNT in procedures you write.


NUMBER? input


NUMBER? can take any kind of input: number, word, or list. It will report back whether the input is a number that you could use in a calculation.

NUMBER? 5
Result: TRUE
NUMBER? "FIVE
Result: FALSE
NUMBER? FIVE
Error: FIVE is not a procedure
NUMBER? [1 2 3 4 5]
Result: FALSE

» Things to Try

How could you use NUMBER? in programs you write?

If you are asking a person to type a number, you might need to check that they didn’t type O instead of 0, for example. See more about Getting User Input.


ABS number


An absolute value of a number reports how far that number is from zero. An absolute value is never a negative number.

ABS 40
Result: 40 ABS -60
Result: 60

» Things to Try

Think of how you might use ABS in a program you write.

If you asked a person to guess a number between -50 and 50 (and you knew the answer), you could tell the person how far off they are by using the absolute value of their guess. Can you figure out how this program works? You might have to learn a few new commands.

CLEARTEXT
MAKE "ANSWER -12
PRINT "|Guess a number between -50 and 50.|
MAKE "GUESS READWORD
(PRINT "|You are| ABS :ANSWER-:GUESS "|away from my number.|)


PI


PI reports the value of the mathematical constant Pi, which is roughly 22/7.

Pi is an irrational number, which means it never repeats or stops.
Some people’s hobby is to memorize the value of Pi to a very large number!

To see what Logo knows about Pi, type this:

PI
Result: 3.14

You might be disappointed that Pi is only 2 digits long after the decimal point.

That is because the precision that Logo starts up using is only 2. The variable :PRECISION sets how much of a decimal number is displayed. The full value of the decimal number will be calculated beyond the number of places that are visible.

To change the precision used, set the :PRECISION variable to a larger number.

:PRECISION
Result: 2
PI
Result: 3.14
MAKE "PRECISION 10
PI
Result: 3.14159265356

» Things to Try

How high can the value of :PRECISION be? Try to figure that out.

Pi is the ratio between the circumference (distance around a circle) and its diameter (the distance across).

The circumference can be calculated using the equation 2 * PI * R, or 2πR, where R is the radius, or half the diameter.

The area of the circle is also calculated using Pi: 2 * PI * R * R, or 2πR², called 2 PI R squared.

You’ll learn about PI in math class at some point. Until then, you can use these equations to explore circles a bit.

If you type STAMPOVAL 50 50 you get a circle with a radius of 50 turtle steps.

Can you use this information to figure out its circumference using the value of PI?

MAKE "PRECISION 10
2 * PI * 50
Result: 314.159265359

Do some more exploring with PI!