Introduction to Computers and Programming Lecture 12: Math.random() Professor: Evan Korth New York University Road Map • Math.random • Reading: – Liang 5: Chapter 4: Section 4.8.5 – Liang 6: Chapter 5: Section 5.9.5 – Liang 7: Chapter 5: Section 5.9.5 review • What is a method? • What information can you learn about a method from its header? • What does it mean to invoke a method? • What is the difference between a formal parameter and an actual parameter? • Why don’t we have to import the Math class? • What is abstraction in computer science? random numbers • Often we want our programs to generate random numbers. – games of chance – testing without user interaction • java.lang.Math provides a method which can be used to generate random numbers 5 Random-Number Generation • Java random-number generators – Math.random() • Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. – What if we want to generate random integers? 2003 Prentice Hall, Inc. All rights reserved. 6 Random-Number Generation – ( int ) ( Math.random() * 6 ) • Produces integers from 0 - 5 2003 Prentice Hall, Inc. All rights reserved. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 7 // Fig. 6.7: RandomIntegers.java // Shifted, scaled random integers. import javax.swing.JOptionPane; Outline RandomIntegers. java public class RandomIntegers { public static void main( String args[] ) { int value; String output = ""; Produce integers in range 1-6 // loop 20 times for ( int counter = 1; counter <= 20; counter++ ) { // pick random integer between 1 and 6 value = 1 + ( int ) ( Math.random() * 6 ); output += value + " "; // append value to output Line 16 Produce integers in range 1-6 Line 16 Math.random returns doubles. We cast the double as an int // if counter divisible by 5, append newline toMath.random String output returns doubles. if ( counter % 5 == 0 ) We cast the double as an int output += "\n"; } // end for 2003 Prentice Hall, Inc. All rights reserved. 26 27 28 29 30 31 32 33 34 8 JOptionPane.showMessageDialog( null, output, "20 Random Numbers from 1 to 6", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); // terminate application Outline RandomIntegers. java } // end main } // end class RandomIntegers 2003 Prentice Hall, Inc. All rights reserved. 9 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 Outline // Fig. 6.8: RollDie.java // Roll a six-sided die 6000 times. import javax.swing.*; RollDie.java public class RollDie { public static void main( String args[] ) { Produce integers int frequency1 = 0, frequency2 = 0, frequency3 = 0, frequency4 = 0, frequency5 = 0, frequency6 = 0, face; Line 14 in range 1-6Produce integers in range 1-6 // summarize results for ( int roll = 1; roll <= 6000; roll++ ) { face = 1 + ( int ) ( Math.random() * 6 ); // determine roll value and increment appropriate counter switch ( face ) { case 1: ++frequency1; break; Lines 17-43 Increment appropriate frequency counter, depending on randomly generated number Increment appropriate frequency counter, depending on randomly generated number case 2: ++frequency2; break; case 3: ++frequency3; break; 2003 Prentice Hall, Inc. All rights reserved. 10 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 case 4: ++frequency4; break; case 5: ++frequency5; break; case 6: ++frequency6; break; } // end switch } // end for JTextArea outputArea = new JTextArea(); outputArea.setText( "Face\tFrequency" + "\n1\t" + frequency1 + "\n2\t" + frequency2 + "\n3\t" + frequency3 + "\n4\t" + frequency4 + "\n5\t" + frequency5 + "\n6\t" + frequency6 ); Outline RollDie.java This is different from the example I showed in the compiler. I left it this way to show you that showMessageDialog() can take other objects besides String for the message. Remember the API. You do not need to know JTextArea for this class! JOptionPane.showMessageDialog( null, outputArea, "Rolling a Die 6000 Times", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); // terminate application } // end main } // end class RollDie 2003 Prentice Hall, Inc. All rights reserved. 11 2003 Prentice Hall, Inc. All rights reserved.