Computer Science 15-110, Spring 2010
Class Notes:  Math and Random Methods


  1. Math Constants
    1. PI
    2. E
  2. Math Methods
    1. abs
    2. ceil
    3. exp
    4. floor
    5. log
    6. log10
    7. max
    8. min
    9. pow
    10. random
    11. round
    12. signum
    13. sqrt
    14. trigonometry methods
    15. Online Math API
  3. Random Methods
    1. nextInt
    2. nextDouble
    3. nextBoolean
    4. setSeed
    5. Online Random API

Math and Random Methods

  1. Math Constants
     
    1. PI
      class MyCode  {
        public static void main(String[] args) {
          System.out.println(Math.PI);
        }
      }
    2. E
      class MyCode  {
        public static void main(String[] args) {
          System.out.println(Math.E);
        }
      }
  2. Math Methods
     
    1. abs
      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
      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...

      class MyCode  {
        public static void main(String[] args) {
          int i = Math.ceil(3.5); // Error: will not compile!
        }
      }
    3. exp
      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
      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
      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
      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
      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...

      class MyCode  {
        public static void main(String[] args) {
          int i = Math.max(1,2,3); // Error: will not compile!
        }
      }
    8. min
      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
      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...

      class MyCode  {
        public static void main(String[] args) {
          int i = Math.pow(2,3); // Error: will not compile!
          System.out.println(i);
        }
      }
    10. random
      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
      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...

      class MyCode  {
        public static void main(String[] args) {
          int i = Math.round(3.5); // Error: will not compile!
        }
      }
    12. signum
      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
      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. nextInt
      import java.util.Random;
      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;
      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;
      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;
      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;
      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;
      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;
      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;
      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

carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem