Random Numbers


Text of video

Download fish-back-forth.a3p

Experiment! Try different ranges of values. Try making the duration a facto9r of the distance with a little bit of randomness. Add a third marker and use a random value to decide wich marker to swim to.

public void myFirstMethod() {
  while ( true ) {
    Double distance = RandomUtilities.nextDoubleInRange( 0.25, this.clownFish.getDistanceTo( this.onLeft ) );
    this.clownFish.turnToFace( this.onLeft );
    this.clownFish.move( MoveDirection.FORWARD, distance );
    this.clownFish.turnToFace( this.onRight );
    distance = RandomUtilities.nextDoubleInRange( 0.25, this.clownFish.getDistanceTo( this.onRight ) );
    this.clownFish.move( MoveDirection.FORWARD, distance );
    }
} 

NEXT: Self study questions