Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computers and Programming Lecture 12: Math.random() Professor: Evan Korth New York University.

Similar presentations


Presentation on theme: "Introduction to Computers and Programming Lecture 12: Math.random() Professor: Evan Korth New York University."— Presentation transcript:

1

2 Introduction to Computers and Programming Lecture 12: Math.random() Professor: Evan Korth New York University

3 Road Map Math.random Reading: –Liang 4: Chapter 4: Section 4.8.4 –Liang 5: Chapter 4: Section 4.8.5

4 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?

5 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

6  2003 Prentice Hall, Inc. All rights reserved. 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?

7  2003 Prentice Hall, Inc. All rights reserved. 6 Random-Number Generation –( int ) ( Math.random() * 6 ) Produces integers from 0 - 5

8  2003 Prentice Hall, Inc. All rights reserved. Outline 7 RandomIntegers. java Line 16 Produce integers in range 1-6 Line 16 Math.random returns double s. We cast the double as an int 1 // Fig. 6.7: RandomIntegers.java 2 // Shifted, scaled random integers. 3 import javax.swing.JOptionPane; 4 5 public class RandomIntegers { 6 7 public static void main( String args[] ) 8 { 9 int value; 10 String output = ""; 11 12 // loop 20 times 13 for ( int counter = 1; counter <= 20; counter++ ) { 14 15 // pick random integer between 1 and 6 16 value = 1 + ( int ) ( Math.random() * 6 ); 17 18 output += value + " "; // append value to output 19 20 // if counter divisible by 5, append newline to String output 21 if ( counter % 5 == 0 ) 22 output += "\n"; 23 24 } // end for 25 Produce integers in range 1-6 Math.random returns double s. We cast the double as an int

9  2003 Prentice Hall, Inc. All rights reserved. Outline 8 RandomIntegers. java 26 JOptionPane.showMessageDialog( null, output, 27 "20 Random Numbers from 1 to 6", 28 JOptionPane.INFORMATION_MESSAGE ); 29 30 System.exit( 0 ); // terminate application 31 32 } // end main 33 34 } // end class RandomIntegers

10  2003 Prentice Hall, Inc. All rights reserved. Outline 9 RollDie.java Line 14 Produce integers in range 1-6 Lines 17-43 Increment appropriate frequency counter, depending on randomly generated number 1 // Fig. 6.8: RollDie.java 2 // Roll a six-sided die 6000 times. 3 import javax.swing.*; 4 5 public class RollDie { 6 7 public static void main( String args[] ) 8 { 9 int frequency1 = 0, frequency2 = 0, frequency3 = 0, 10 frequency4 = 0, frequency5 = 0, frequency6 = 0, face; 11 12 // summarize results 13 for ( int roll = 1; roll <= 6000; roll++ ) { 14 face = 1 + ( int ) ( Math.random() * 6 ); 15 16 // determine roll value and increment appropriate counter 17 switch ( face ) { 18 19 case 1: 20 ++frequency1; 21 break; 22 23 case 2: 24 ++frequency2; 25 break; 26 27 case 3: 28 ++frequency3; 29 break; 30 Produce integers in range 1-6 Increment appropriate frequency counter, depending on randomly generated number

11  2003 Prentice Hall, Inc. All rights reserved. Outline 10 RollDie.java 31 case 4: 32 ++frequency4; 33 break; 34 35 case 5: 36 ++frequency5; 37 break; 38 39 case 6: 40 ++frequency6; 41 break; 42 43 } // end switch 44 45 } // end for 46 47 JTextArea outputArea = new JTextArea(); 48 49 outputArea.setText( "Face\tFrequency" + "\n1\t" + frequency1 + 50 "\n2\t" + frequency2 + "\n3\t" + frequency3 + 51 "\n4\t" + frequency4 + "\n5\t" + frequency5 + 52 "\n6\t" + frequency6 ); 53 54 JOptionPane.showMessageDialog( null, outputArea, 55 "Rolling a Die 6000 Times", JOptionPane.INFORMATION_MESSAGE ); 56 57 System.exit( 0 ); // terminate application 58 59 } // end main 60 61 } // end class RollDie 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!

12  2003 Prentice Hall, Inc. All rights reserved. 11


Download ppt "Introduction to Computers and Programming Lecture 12: Math.random() Professor: Evan Korth New York University."

Similar presentations


Ads by Google