CS 100: Lecture L5
February 9
Computing averages, max's and min's
// L5A: Compute the average, max and min of one million numbers that // are selected randomly from the interval [0,1). import java.io.*; public class L5A { public static void main(String args[]) { TokenReader in = new TokenReader(System.in); int N = 1000000; // Number of trials int k; // A counter. double x; // A "random number" /************************************************************ Computing an average of N random numbers. */ double sum = 0; // Initialize the "running sum" variable. for (k=1;k<=N;k=k+1) { x = Math.random(); sum = sum+x; // Update the running sum. } double ave = sum/N; System.out.println("Average = " + ave); /************************************************************ Computing the minimum of N random numbers. */ double minVal; /* Recognizing that the minimum cannot be any larger than unity, we initialize minVal to 1 and then "look" for something smaller. This works because we know that all the random numbers are between 0 and 1: */ minVal = 1.0; for (k=1;k<=N;k=k+1) { x = Math.random(); if (x<minVal) { minVal = x; } } System.out.println("Min = " + minVal); /* To use the previous technique in a general look-for-the-min problem, you need an upper bound for the values searched. minVal is then initialized to that upper bound. Some applications it is hard to deduce an upper bound. The following approach can then be used: */ x = Math.random(); minVal = x; // Initialize minVal to the "first value" for (k=2;k<=N;k=k+1) { x = Math.random(); if (x<minVal) { minVal = x; } } /************************************************************ Computing the maximum of N random numbers. */ double maxVal; /* Recognizing that the maximum cannot be any smaller than zero, we initialize maxVal to 0 and then "look" for something bigger. This works because we know that all the random numbers are between 0 and 1: */ maxVal = 0.0; for (k=1;k<=N;k=k+1) { x = Math.random(); if (x>maxVal) { maxVal = x; } } System.out.println("Max = " + maxVal); /* To use the previous technique in a general look-for-the-max problem, you need a lower bound for the values searched. maxVal is then initialized to that lower bound. In some applications it is hard to deduce a lower bound. The following approach can then be used: */ x = Math.random(); maxVal = x; // Initialize maxVal to the "first value" for (k=2;k<=N;k=k+1) { x = Math.random(); if (x>maxVal) { maxVal = x; } } /* We mention that 5 sequences of random numbers were generated in the above program, Each involved N numbers. */ in.waitUntilEnter(); } }
Practice with the && and ||
// L5B: Some 3-dice games import java.io.*; public class L5B { public static void main(String args[]) { TokenReader in = new TokenReader(System.in); final int N = 1000; // The number of trials int k; // An index int d1, d2, d3; // The value of the three dice rolls. int count0 = 0; // The number of times either d1 or d2 odd. int count1 = 0; // The number of times d1, d2, and d3 are all different. int count2 = 0; // The number of times d1, d2, and d3 sum to seven int count3 = 0; // The number of times one throw equals the sum of the other two. for (k=1;k<=N;k=k+1) { // Generate three dice rolls: d1 = (int)( 1.0 + Math.floor(6*Math.random())); d2 = (int)( 1.0 + Math.floor(6*Math.random())); d3 = (int)( 1.0 + Math.floor(6*Math.random())); /* Here is why each of these variables contain a randomly selected integer from the set {1,2,3,4,5,6}? Math.random() is a random number between 0 and 1 (excluding 1) 6*Math.random() is a random number between 0 and 6 (excluding 6) Math.floor(6*Math.random()) is one of 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 with equal probability 1.0+Math.floor(6*Math.random()) is one of 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 with equal probability. The final (int) cast represents this value as an integer type. */ if (d1%2==1 || d2%2==1) { // Either the first or second roll is odd-valued. count0 = count0+1; } if ((d1!=d2) && (d1!=d3) && (d2!=d3)) { // All three are different count1 = count1+1; } if (d1+d2+d3 == 7) { // The sum is equal to 7 count2 = count2+1; } if ((d1==d2+d3) || (d2==d1+d3) || (d3==d1+d2)) { // One throw is the sum of the other two. count3 = count3+1; } } System.out.println("Number of times either the first or second roll is odd = " + count0); System.out.println("Number of times the 3 rolls are different = " + count1); System.out.println("Number of times the 3 rolls sum to seven = " + count2); System.out.println("Number of times one roll is the sum of the other two = " + count3); in.waitUntilEnter(); } }
A Simulation under the control of a While Loop
// Example L5C: Two-dimensional random walk. import java.io.*; import java.awt.*; public class CUCSDrawing extends Frame { final int hc = 500; final int vc = 350; // (hc,vc) is the starting point. final int s = 1; // Step length parameter final int nMax = 200; // Maximum number of steps. final int r = 300; // Escape radius. final double pRight = 0.6; // Probability of moving right in a given step. final double pDown = 0.5; // probability of moving down in a given step. public void paint(Graphics g) { // Draw the playing field g.setColor(Color.magenta); g.fillRect(0,0,1000,1000); g.setColor(Color.pink); g.fillOval(hc-r,vc-r,2*r,2*r); g.setColor(Color.black); g.drawLine(hc-r,vc,hc+r,vc); g.drawLine(hc,vc-r,hc,vc+r); int h,v; // (h,v) is the current location int hNext,vNext; // (hNext,vNext) is the next location double d=0.0; // Distance from the starting point int k =0; // Step counter. h = hc; v = vc; while (d<=r && k<=nMax) // Still inside the circle so take a step. { if (Math.random()<= pRight) // With probability pRight we move s units right. Else, move s units left. { hNext = h+s; } else { hNext = h-s; } if (Math.random()<= pDown) // With probability pDown we move s units down. Else move s units up. { vNext = v+s; } else { vNext = v-s; } // Draw a line to the next point and compute its distance to (hc,vc) g.drawLine(h,v,hNext,vNext); h = hNext; v = vNext; d = Math.sqrt(Math.pow(h-hc,2) + Math.pow(v-vc,2)); k=k+1; } } } public class L5C { public static void main(String args[]) { CUCSDrawing d = new CUCSDrawing(); d.resize(1000,800); d.move(0,75); d.setTitle("L5C"); d.show(); d.toFront(); } }