We look at two commands, randomX and randomY. Each returns, or outputs, a random x- or y-coordinate respectively. So this program line:
x = randomX();
Places a value into the variable x that is between worldXMin and worldXMax. Actually, x could contain the value of worldXMin up to but not including worldXMax. If the x-axis had its bounds set up to be from -10 to 10, then x could contain values from -10 to 9.9999....
The command randomY works the same way, except it returns values relative to the y-axis.
If you change the bounds of the graph, these two functions will appropriately change the way they return values.
Note that these commands are different than many that we have seen. These return meaningful values. The command drawLineSegment, for example, does not return any meaningful value that would be placed in a variable.
Be sure to note the necessary parentheses when using randomX and randomY.
You can also create random numbers over any range. The randomRealRange command is used for this:
So, if you wanted to generate a random number somewhere between 2 and 4, then this program line would place that value into the variable a:
a = randomRealRange(2, 4);
Actually, the above program line will place a random value into a over a range from and including 2 up to but not including 4. That is, the range would be 2 to 3.9999....
This will work with negative and positive numbers, just as long as the first value is less than the second value.
This program is showing two basic procedures:
This is our first animating EZ Math Movie. You will need to click the 'Stop' button to halt this program.
Also, note that this program has some code in the before loop section. Usually, as here, when a program is the animating type, all setup code goes in the before loop section. We really do not want to clear the graph and printer at the start of each loop cycle. We want to do those things once in the before loop section, and then let the points pile up on the graph and the numbers scroll down the printer.
Understand that these are not related procedures. The points on the scattering around the graph have no connection to the numbers pouring out of the printer. The program is just showing two ways that EZ Math Movie makes random numbers.
Don't forget to press 'Stop', or the program will run endlessly.
Next tutorial: Powers, square roots
Here are some links to related articles at EZ Programing Demos: