1. Math Constants
     
    1. PI
      public class MyCode { public static void main(String[] args) { System.out.println(Math.PI); } }
    2. E
      public class MyCode { public static void main(String[] args) { System.out.println(Math.E); } }
  2. Math Methods
     
    1. abs
      public class MyCode { public static void main(String[] args) { System.out.println(Math.abs(5)); System.out.println(Math.abs(-5)); System.out.println(Math.abs(5.0)); System.out.println(Math.abs(-5.0)); } }
    2. ceil
      public class MyCode { public static void main(String[] args) { System.out.println(Math.ceil(2.01)); System.out.println(Math.ceil(2.00)); System.out.println(Math.ceil(1.99)); System.out.println(Math.ceil(-2.01)); System.out.println(Math.ceil(-2.00)); System.out.println(Math.ceil(-1.99)); } }

      A common mistake...

      public class MyCode { public static void main(String[] args) { int i = Math.ceil(3.5); // Error: will not compile! } }
    3. exp
      public class MyCode { public static void main(String[] args) { System.out.println(Math.E); System.out.println(Math.exp(1)); System.out.println(Math.exp(0)); System.out.println(Math.exp(2)); System.out.println(Math.pow(Math.E, 2)); } }
    4. floor
      public class MyCode { public static void main(String[] args) { System.out.println(Math.floor(2.01)); System.out.println(Math.floor(2.00)); System.out.println(Math.floor(1.99)); System.out.println(Math.floor(-2.01)); System.out.println(Math.floor(-2.00)); System.out.println(Math.floor(-1.99)); } }
    5. log
      public class MyCode { public static void main(String[] args) { double eSquared = Math.exp(2); double eCubed = Math.exp(3); System.out.println(Math.log(Math.E)); System.out.println(Math.log(eSquared)); System.out.println(Math.log(eCubed)); System.out.println(Math.log(Math.exp(10))); System.out.println(Math.log(1)); System.out.println(Math.log(0)); // doesn't crash! System.out.println(Math.log(-1)); // ditto (yet in a different way) } }
    6. log10
      public class MyCode { public static void main(String[] args) { System.out.println(Math.log10(0.001)); System.out.println(Math.log10(0.1)); System.out.println(Math.log10(10)); System.out.println(Math.log10(1000)); } }
    7. max
      public class MyCode { public static void main(String[] args) { System.out.println(Math.max(1, 2)); System.out.println(Math.max(-1, -2)); System.out.println(Math.max(1.0, 2.0)); System.out.println(Math.max(1.0, 2)); } }

      A common mistake...

      public class MyCode { public static void main(String[] args) { int i = Math.max(1,2,3); // Error: will not compile! } }
    8. min
      public class MyCode { public static void main(String[] args) { System.out.println(Math.min(1, 2)); System.out.println(Math.min(-1, -2)); System.out.println(Math.min(1.0, 2.0)); System.out.println(Math.min(1.0, 2)); } }
    9. pow
      public class MyCode { public static void main(String[] args) { System.out.println(Math.pow(2,3)); System.out.println(Math.pow(2,2)); System.out.println(Math.pow(2,1)); System.out.println(Math.pow(2,0.5)); System.out.println(Math.pow(2,0)); System.out.println(Math.pow(2,-1)); } }

      A common mistake...

      public class MyCode { public static void main(String[] args) { int i = Math.pow(2,3); // Error: will not compile! System.out.println(i); } }
    10. random
      public class MyCode { public static void main(String[] args) { System.out.println(Math.random()); System.out.println(Math.random()); System.out.println(Math.random()); System.out.println(Math.random()); System.out.println(Math.random()); } }
    11. round
      public class MyCode { public static void main(String[] args) { System.out.println(Math.round(2.51)); System.out.println(Math.round(2.50)); System.out.println(Math.round(2.49)); System.out.println(Math.round(-2.51)); System.out.println(Math.round(-2.50)); // surprise? System.out.println(Math.round(-2.49)); } }

      A common mistake...

      public class MyCode { public static void main(String[] args) { int i = Math.round(3.5); // Error: will not compile! } }
    12. signum
      public class MyCode { public static void main(String[] args) { System.out.println(Math.signum(42)); System.out.println(Math.signum(0)); System.out.println(Math.signum(-42)); } }
    13. sqrt
      public class MyCode { public static void main(String[] args) { System.out.println(Math.sqrt(4)); System.out.println(Math.sqrt(-4)); // surprise? } }
    14. trigonometry methods
      Note:  You are not responsible for the trigonometry methods (sin, cos, tan, asin, acos, atan, atan2, toDegrees, and toRadians), except those few methods which we will use (sparingly, and with clear explanations) when we cover graphics.
       
    15. Online Math API
      See:  http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Math.html
       
  3. Random Methods
     
    1. nextIntimport java.util.Random;
      public class MyCode { public static void main(String[] args) { Random random = new Random(); System.out.println(random.nextInt()); System.out.println(random.nextInt()); System.out.println(random.nextInt()); } }

      Another Example:

      import java.util.Random; public class MyCode { public static void main(String[] args) { Random random = new Random(); System.out.println("Randoms between about -2b and +2b:"); for (int i=0; i<5; i++) System.out.println(random.nextInt()); System.out.println(); System.out.println("Randoms between 0 (inclusive) and 10 (exclusive):"); for (int i=0; i<40; i++) { System.out.print(random.nextInt(10)); System.out.print(" "); } System.out.println(); } }

      A More Interesting Example (Odds of Rolling a 7):

      import java.util.Random; public class MyCode { public static void main(String[] args) { Random random = new Random(); System.out.println("This program estimates the odds of rolling a 7 on two dice."); int rolls = 1000000; int sevens = 0; for (int roll=0; roll<rolls; roll++) { int die1 = 1 + random.nextInt(6); int die2 = 1 + random.nextInt(6); if ((die1 + die2) == 7) sevens++; } System.out.println("Odds = " + sevens + "/" + rolls + " = " + (100.0*sevens/rolls) + "%"); } }
    2. nextDouble
      import java.util.Random; public class MyCode { public static void main(String[] args) { Random random = new Random(); System.out.println(random.nextDouble()); System.out.println(random.nextDouble()); System.out.println(random.nextDouble()); } }

      A More Interesting Example (Expected difference of two randoms in [0,1]):

      import java.util.Random; public class MyCode { public static void main(String[] args) { Random random = new Random(); System.out.println("This program estimates the expected difference"); System.out.println("between two random numbers between 0 and 1."); int attempts = 1000000; double sumOfDifferences = 0; for (int attempt=0; attempt<attempts; attempt++) { double d1 = random.nextDouble(); double d2 = random.nextDouble(); sumOfDifferences += Math.abs(d2 - d1); } System.out.println("Expected difference = " + (sumOfDifferences / attempts)); } }
    3. nextBoolean
      import java.util.Random; public class MyCode { public static void main(String[] args) { Random random = new Random(); System.out.println(random.nextBoolean()); System.out.println(random.nextBoolean()); System.out.println(random.nextBoolean()); } }

      A More Interesting Example (Odds of Getting 1 Heads and 2 Tails):

      import java.util.Random; public class MyCode { public static void main(String[] args) { Random random = new Random(); System.out.println("This program estimates the odds of flipping 3 coins"); System.out.println("And getting exactly 1 heads and 2 tails."); int attempts = 1000000; int successes = 0; for (int attempt=0; attempt<attempts; attempt++) { boolean heads1 = random.nextBoolean(); boolean heads2 = random.nextBoolean(); boolean heads3 = random.nextBoolean(); if ((heads1 && !heads2 && !heads3) || (heads2 && !heads1 && !heads3) || (heads3 && !heads1 && !heads2)) successes++; } System.out.println("Odds = " + successes + "/" + attempts + " = " + (100.0*successes/attempts) + "%"); } }
    4. setSeed
      import java.util.Random; public class MyCode { public static void main(String[] args) { Random random = new Random(); System.out.println("These values change on each run:"); System.out.println(random.nextInt(100)); System.out.println(random.nextInt(100)); System.out.println(); System.out.println("But these values DO NOT CHANGE on each run:"); System.out.println("(which is very handy for debugging!)"); random.setSeed(42); System.out.println(random.nextInt(100)); System.out.println(random.nextInt(100)); } }
    5. Online Random API
      See:  http://java.sun.com/j2se/1.5.0/docs/api/java/util/Random.html