In this lesson, students will learn how to write programs that generate and display random numbers. |
Students will be able to:
|
1. Random Numbers It is often useful to have the computer generate one or more random numbers for us. A random number is a number that could be any number--we don't know what it is in advance, although usually we specify a range of possibilities. It's like when we say "Pick a number from 1 to 100." We don't know in advance which number the person will pick, but we do know that it will be from 1 to 100. Random numbers can be used in many different ways in programming. For example, we might want the robot to move to a sequence of random positions around the grid. Or, if we have an activity that asks the user to input a value, we can test the code with different values by simulating the user's value with randomly generated numbers. RoboBlocky has several code blocks in the Math menu that will generate random numbers. In this lesson we'll look at two of them.
2. Generating Random Integers The random integer block will generate a random integer within a range of two specified values, such as from 1 to 100:
3. Generating Random Decimal Values The random double block will generate a random decimal value within a range of two specified values, such as from 0 to 10: The double value will be generated with 16 digits, such as 8.375339510276483. If we want to display the value but not display all 16 digits, we can use the set digits after decimal block (in the Text menu). Or we can use the round digits after decimal point block (in the Math menu) to round the number to a specified number of digits. (Note that the set digits after decimal operation does not change the value of the number, but just determines how many digits to display; the number will still have 16 digits. But rounding the number does change its value. Remember also that "double" refers to how decimal numbers are stored within the computer's memory. It does not have anything to do with doubling a number.)
|
|
The code generates a random integer from 1 to 100, stores it in a variable, and displays the value in a message box. |
The code generates a random decimal value from 0 to 10, stores it in a variable, and displays the value in a message box. Then it sets the number of decimal places to display to 2, and displays the value again. Finally, it generates another random decimal value from -1 to 1 and displays it to 3 decimal places. |
The code generates a random value from 5 to 11, stores it in a variable named distance, and displays a point for the distance on the number line. It then drives the robot that far on the number line. |
The code generates two random values from -10 to 10, stores them in variables named xcoordinate and ycoordinate, and displays them in message boxes. It then draws a yellow star at that (xcoordinate, ycoordinate) location and drives the robot there. |
Grid Size: S M L | Simple View: | MinView: |
Coord: x-axis y-axis | Grid Lines: x-axis y-axis | |||
Grid: | 12x12 inches | 24x24 inches | 36x36 inches | |
72x72 inches | 96x96 inches | 192x192 inches | ||
1x1 inches | 2x2 inches | Fraction: | ||
xy-Range: | ||||
|
||||
|
||||
Quad: | 1 Quadrant | 4 Quadrants | 1&4 Quadrants | |
Units: | US Customary | Metric | Ruler: |
Labels: |
|
|
|
Font
px
|
|
Tics Lines: |
|
Width
px
|
Hash Lines: |
|
Width
px
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
(
,
in
) in
|
|
![]() |
|
|
|||||||||||
|
|||||||||||