How to Scatter Turtles in MicroWorld EX
Games and simulations that use MicroWorld EX often require the random distribution or spreading out of objects across the field of action. The setx random 640 command and the sety random 480 command place the turtle at a random location in the 640 by 480 pixel workspace.
You can use a similar structure for distributing objects in other programming languages by replacing 640 with the maximum dimension along the x-axis and replacing 480 with the maximum dimension along the y-axis.
One potentially confusing aspect of this is that the x-coordinates of the workspace go from –319 to +320, but the setx random 640 command generates numbers from 0 to 639. You may be wondering why these two numbers don’t match up!
If setx random 640 generates a number from 0 to 320, the turtle simply sets its x-coordinate to that value. But if setx random 640 generates a number greater than 320, the turtle is pushed off the right side of the workspace to wrap around, reappearing on the left side of the workspace. If setx random 640 generates an x-coordinate of 321, the turtle appears at an x-coordinate of –319.
If setx random 640 generates an x-coordinate of 421, the turtle appears at an x-coordinate of –219. In this way, the turtle can end up anywhere with any possible x-coordinate in the workspace. The y-coordinates of the workspace go from –239 to +240 and the sety random 480 command works to position the turtle randomly in the y-direction in much the same way.