Turtlegraphics for Smartphones and Tablets

Bern University of Teacher Education  
HomeStart Online EditorPrintAndroid GamesJava-Online

Use random numbers


Random numbers are often used with simulations and various statistic methods.Also in the turtle graphic, interesting applications can be programmed using random numbers. The method Math.random() provides a decimal random number between 0 and 1.

In the frist example stars are drawn in coincidentally selected positions. With a simple calculation using such random numbers, coordiantes between -180 and 180 result.

Edit source with online-compiler

App installieren auf Smartphone oder Tablet

QR-Code

download sources (Tu15.zip)

// Tu15.java

package app.tu15;

import turtle.*;

public class Tu15 extends Playground
{
  public void main()
  {
    for (int = 0; i < 50; i++)
    {  
      double zx = 360 * Math.random();
      double zy = 360 * Math.random();
      setPos(-180 + zx, -180 + zy);
      star();
    }    
  }
  
  void star()
  { 
    setPenColor(YELLOW);
    fillToPoint(getX(), getY());
    for (int = 0; i < 6; i++)
    {
      fd(10);
      rt(140);
      fd(10);
      left(80);
    }  
  }
}
 

Explanatations to the program code:

double zx = 360 * Math.random()

Creates a random number between 0 and 360

setPos(180 - zx, 180 - zy)

-180 + zx results in a random number between -180 and 180 (x-Koordinate)
-180 + zy results in a random number between -180 and 180 (y-Koordinate)

fillToPoint(getX(), getY())

Fills the figure from the current point

 

Confused turtle

A turtle looks for its nest. This is the procedure: It coincidentally chooses a direction and moves 20 steps straight ahead. At the end of the route it chooses again randomly a direction and moves again 20 steps straight ahead. If it is only35 steps or closer to the nest, it moves directly to the nest. The nest draws the global turtle.

Edit source with online-compiler

Install App on smartphone or tablet

QR-Code

download sources (Tu15a.zip)

// Tu15a.java
package app.tu15a;

import turtle.*;

public class Tu15a extends Playground
{
   public void main()
  {
    turtleNest();
    int xHome = -100;
    int yHome = 100;
    int nbStep = 0;
    Turtle = new Turtle(RED);
    t.st();
    t.setSpeed(10);
    while (t.distance(xHome, yHome) > 35)
    {
      t.lt((int) (Math.random() * 180) - 90);
      t.fd(20);
      nbStep++;
      if (t.isOnBorder())
        t.rt(180).fd(25);
    }  
    t.setPos(xHome, yHome);
    label("Home found!(" + nbStep + " Steps)");
  }

  void turtleNest()
  {
    setPenColor(YELLOW);
    setPos(-104, 100);
    for (int = 0; i < 36; i++)
    {
      fd(3);
      rt(10);
    }
    fill(getX() + 2, getY() + 2);
  }
}
 

Explanatations to the program code:

distance(xHome, yHome)

Returns the distance between the current turtle posistion and the nest

Math.random() * 180) - 90

A random number between -90 and 90

isOnBorder() Damit die Turtle den Playground nicht verlässt, kehrt sie um, sobald sie den Rand berührt

 

Calculation of the number PI with the Monte-Carlo Method

Random points, whose coordinates are determined by two random numbers, are distributed on a square with the side lenght of 1. From the ratio of the number of the points that lie within the quarter circle, the number PI can be calculated.

Edit source with online-compiler

Install App on smartphone or tablet

QR-Code

download sources (MonteCarlo.zip)

// MonteCarlo.java

package app.montecarlo;

import turtle.*;

public class MonteCarlo extends Playground
{
  public void main()
  {
    double zx, zy;
    int nbHit = 0;
    int nbDots = 30000;
    for (int = 0; i < nbDots; i++)
    {
      zx = Math.random();
      zy = Math.random();
      
      if (zx * zx + zy * zy < 1)
      {  
        setPenColor(RED);
        nbHit++;
      }
      else
        setPenColor(GREEN);
      setPos(320 * zx - 160, 320 * zy - 160);
      dot();
    }
    setPos(-40-185);
    label("pi = " + nbHit * 4.00/ nbDots);
  }
}
 

Explanation to the program code:

zx = Math.random()
zy = Math.random()

Two random numbers between 0 and 1 are created

if (zx * zx + zy * zy < 1)

According the theorem of Pythagoras: If the sum of the squares from both coordinates equals 1, the point lies on the circle. If < the point lies within the quarter circle and is counted as a hit.

setPos(320 * zx - 160, 320 * zy - 160)

For the graphical presentation, the coordinates are converted in a way that they are well representable in the turtle window

dot() Draws a point onthe current position of the turtle
label("pi = " + nbHit * 4.00/ nbDots) By using the method label() texts and values of variables can be displayed