Computer Science 15-100, Spring 2009
Class Notes: Math and Random Methods
Math and Random Methods
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));
}
}
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!
}
}
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));
}
}
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));
}
}
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)
}
}
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));
}
}
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!
}
}
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));
}
}
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);
}
}
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());
}
}
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!
}
}
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));
}
}
class MyCode {
public static void main(String[] args) {
System.out.println(Math.sqrt(4));
System.out.println(Math.sqrt(-4)); // surprise?
}
}
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) + "%");
}
}
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));
}
}
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) + "%");
}
}
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));
}
}
carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem