6 9 Case Study RandomNumber Generation 1 Randomnumber

6. 9 Case Study: Random-Number Generation 1 • Random-number (隨機數字) generation 有兩個方 式: – static method random from class Math • Math. random( ) • Returns doubles in the range 0. 0 x < 1. 0 – Thru object of class Random from package java. util • Can produce pseudorandom boolean, byte, float, double, int, long and Gaussian values – 如 next. Int()、next. Double() 等方法(請參見範例) • Is seeded with the current time of day to generate different sequences of numbers each time the program executes,種子數 用來決定第 1 個隨機數字的值,第 2個隨機數字的值由第 1個 隨機數字的值決定,之後的值都由前一個值決定 1992 -2007 Pearson Education, Inc. All rights reserved.

Outline 2 Import class Random from the java. util package Create a Random object Random. Integers. java (1 of 2) Generate a random die roll 1992 -2007 Pearson Education, Inc. All rights reserved.

Outline 3 Two different sets of results containing integers in the range 1 -6 Random. Integers. java (2 of 2) 1992 -2007 Pearson Education, Inc. All rights reserved.

Outline 4 Import class Random from the java. util package Roll. Die. java (1 of 2) Create a Random object Declare frequency counters 1992 -2007 Pearson Education, Inc. All rights reserved.

Iterate 6000 times Outline 5 Roll. Die. java Generate a random (2 ofdie 2) roll switch based on the die roll 1992 -2007 Pearson Education, Inc. All rights reserved.

Outline Display die roll frequencies 6 Roll. Die. java (3 of 3) 1992 -2007 Pearson Education, Inc. All rights reserved.

6. 9. 1 Generalized Scaling and Shifting of Random Numbers 7 • To generate a random number in certain sequence or range (有規律的數字序列才可以) – Use the expression shifting. Value + difference. Between. Values *random. Numbers. next. Int( scaling. Factor ) where: • shifting. Value (初值) is the first number in the desired range of values • difference. Between. Values (差值) represents the difference between consecutive numbers in the sequence • scaling. Factor (個數) specifies how many numbers are in the range – 例如:介於 3 到 10 間的整數 • 3 + 1 * random. Numbers. next. Int(8) – 例如:由 7, 14, 21, 28, 35 間隨機產生 • 7 + 7 * random. Numbers. next. Int(5) – 那怎麼處理沒有規律的數字序列,例如 3, 5, 13, 15, 37 用 33, switch 1992 -2007 Pearson Education, Inc. All rights reserved.

8 練習 • 以 random number 表示以下範圍的整數: (a) (b) (c) (d) (e) (f) (g) (h) (i) 1 n 2 1 + r 1. next. Int(2) 1 n 100 1 + r 1. next. Int(100) 0 n 9 r 1. next. Int(10) 1000 n 1112 1000 + r 1. next. Int(13) -1 n 1 -1 + r 1. next. Int(3) -3 n 11 -3 + r 1. next. Int(15) 2, 4, 6, 8, 10 2+2*r 1. next. Int(5) 3, 5, 7, 9, 11 3+2*r 1. next. Int(5) 6, 10, 14, 18, 22 6 + 4 * r 1. next. Int(5) • 四捨五入到整數 Math. round( x ) or Math. floor( x + 0. 5) • 四捨五入到小數點後第 1 位,請類推四捨五入到小數點後第 n 位 Math. floor( 10 * (x + 0. 05) ) / 10. 0 1992 -2007 Pearson Education, Inc. All rights reserved.

6. 9. 2 Random-Number Repeatability for Testing and Debugging 9 • To get a Random object to generate the same sequence of random numbers every time the program executes, seed it with a certain value – When creating the Random object: Random random. Numbers = new Random( seed. Value ); – Use the set. Seed method: random. Numbers. set. Seed( seed. Value ); – seed. Value should be an argument of type long 1992 -2007 Pearson Education, Inc. All rights reserved.

10 Error-Prevention Tip 6. 2 While a program is under development(還在開發中), create the Random object with a specific seed value to produce a repeatable ( 可重覆的) sequence of random numbers each time the program executes. If a logic error occurs, fix the error and test the program again with the same seed value-this allows you to reconstruct the same sequence of random numbers that caused the error. Once the logic errors have been removed, create the Random object without using a seed value, causing the Random object to generate a new sequence of random numbers each time the program executes. 1992 -2007 Pearson Education, Inc. All rights reserved.

Outline 11 Import class Random from the java. util package Craps. java (1 of 4) Create a Random object Declare an enumeration Declare constants 1992 -2007 Pearson Education, Inc. All rights reserved.

Outline 12 Call roll. Dice method Craps. java (2 of 4) Player wins with a roll of 7 or 11 Player loses with a roll of 2, 3 or 12 Set and display the point 1992 -2007 Pearson Education, Inc. All rights reserved.

Outline 13 Call roll. Dice method Craps. java (3 of 4) Player wins by making the point Player loses by rolling 7 Display outcome 1992 -2007 Pearson Education, Inc. All rights reserved.

Declare roll. Dice method Outline 14 Craps. java (4 of 4) Generate two dice rolls Display dice rolls and their sum 1992 -2007 Pearson Education, Inc. All rights reserved.

Outline 15 Craps. Test. ja va (1 of 2) 1992 -2007 Pearson Education, Inc. All rights reserved.

6. 10 Case Study: A Game of Chance (Introducing Enumerations) 16 • Enumerations – – Programmer-declared types consisting of sets of constants enum keyword A type name (e. g. Status) Enumeration constants (e. g. WON, LOST and CONTINUE) • cannot be compared against ints 1992 -2007 Pearson Education, Inc. All rights reserved.

17 Good Programming Practice 6. 3, 6. 4 Use only uppercase letters in the names of constants. This makes the constants stand out in a program and reminds the programmer that enumeration constants are not variables. Using enumeration constants (like Status. WON, Status. LOST and Status. CONTINUE) rather than literal integer values (such as 0, 1 and 2) can make programs easier to read and maintain. 1992 -2007 Pearson Education, Inc. All rights reserved.

18 6. 11 Scope of Declarations (宣告的有效範圍) • Basic scope rules – Scope of a parameter declaration (方法參數) is the body of the method in which appears – Scope of a local-variable (區域變數) declaration is from the point of declaration to the end of that block (區塊) – Scope of a local-variable declaration in the initialization section of a for header is the rest of the for header and the body of the for statement – Scope of a method or field of a class is the entire body of the class 1992 -2007 Pearson Education, Inc. All rights reserved.

19 6. 11 Scope of Declarations (Cont. ) • Shadowing (覆蓋) – A field is shadowed (or hidden,不能用) if a local variable or parameter has the same name as the field • This lasts until the local variable or parameter goes out of scope 1992 -2007 Pearson Education, Inc. All rights reserved.

20 Common Programming Error 6. 10 A compilation error occurs when a local variable is declared more than once in a method. Use different names for fields and local variables to help prevent subtle logic errors that occur when a method is called and a local variable of the method shadows a field of the same name in the class. (範例 6 -11) 1992 -2007 Pearson Education, Inc. All rights reserved.

Outline 21 Scope. java (1 of 2) Shadows field x Display value of local variable x 1992 -2007 Pearson Education, Inc. All rights reserved.

Outline Shadows field x 22 Scope. java (2 of 2) Display value of local variable x Display value of field x 1992 -2007 Pearson Education, Inc. All rights reserved.

Outline 23 Scope. Test. jav a 1992 -2007 Pearson Education, Inc. All rights reserved.


25 6. 12 Method Overloading • Method overloading (多載) – Multiple methods with the same name, but different types, number or order of parameters in their parameter lists – Compiler decides which method is being called by matching the method call’s argument list to one of the overloaded methods’ parameter lists • A method’s name and number, type and order of its parameters form its signature – Differences in return type are irrelevant (不相干) in method overloading • Overloaded methods can have different return types • Methods with different return types but the same signature cause a compilation error 1992 -2007 Pearson Education, Inc. All rights reserved.

Outline 26 Correctly calls the “square of int” method Method. Overloa d. java Correctly calls the “square of double” method Declaring the “square of int” method Declaring the “square of double” method 1992 -2007 Pearson Education, Inc. All rights reserved.

Outline 27 Method. Overload. Te st. java 1992 -2007 Pearson Education, Inc. All rights reserved.

Outline 28 Method. Overload Error. java Same method signature Compilation error 1992 -2007 Pearson Education, Inc. All rights reserved.

29 Common Programming Error 6. 11 Declaring overloaded methods with identical parameter lists is a compilation error regardless of whether the return types are different. 1992 -2007 Pearson Education, Inc. All rights reserved.



32 期末重點提示 if (含 logical operators) while for switch break, continue 物件導向 (方法、實體變數、static) Math, Random enum overload argument promotion, type casting precedence and associativity of operators 1992 -2007 Pearson Education, Inc. All rights reserved.

- Slides: 33