N.B. THIS LESSON IS OPTIONAL (While Math.random might be included on the AP exam, the Random class is not part of the AP subset.)
Write expressions that generate a random integer between any two values.
Complete Practice questions
Read BJP 5.1 “Random Numbers”
Complete programming project 1
Projector and computer
Whiteboard and markers
Bell-work and attendance
Introduction and think-pair-share
Student Practice activity
Hook your class by talking about randomized tasks that arise in games they may play. For example:
Create an enemy that spawns after some random amount of time, but between 7 and 23 seconds.
Pick a random item from a list of 100 words the user needs to guess in Hangman
Create a new username between 6 and 10 characters picking randomly what each character is, from lower/capital letters and numbers.
Make a ball bounce, but not precisely, by adding random angles to the bounce.
Once students have all had a chance to explore the dice, show them how to generate random numbers that they can use in their game program, computer simulations, or security programs (moving forward).
Construct a random object (see if anyone can tell you how to do this from their reading last night):
Random r = new Random();
Call the nextInt method, passing the upper limit of the range of your random number (in the case of a 6 sided dice, this would be 6).
result = r.nextInt(6); // Gives you a random number between 0 - 5.
Does this code accurately simulate a six sided dice? (Have students with the six-sided dice answer your question.) To shift that result between 1 and 6, you need to add 1 to your code:
result = r.nextInt(6) + 1; // Gives a random number between 1 - 6.
Output the result:
System.out.println("You rolled a " + result);
Give students a few minutes to do a Think-Pair-Share for the following questions:
Write an expression that simulates a roll of a 20 – sided dice.
Write an expression that yields a random number between 0.0 and 1.0.
Review Table 5.2 from the book as you discuss the answers in a whole group.
nextInt() = random integer between -2^31 and (2^31 – 1)
nextInt(max) = random integer between 0 and (max – 1)
nextDouble() = random real number between 0.0 and 1.0
nextBoolean() = random logical value of true or false
Priming the loop: remind students that they must initialize variables before the loop (this
makes sure that Java knows how to enter/start the loop).
Have students complete the following self-check problems:
a. Self-Check 5.5: randomRangeABCDE b. Self-Check 5.6: randomInteger0to10 c. Self-Check 5.7: randomOddInteger50to99
If students complete these problems with time to spare, have them complete exercises:
a. Exercise 5.4: randomX b. Exercise 5.5: randomLines
If students are having difficulty tracing while loops, using proper syntax, or predicting the output of the loop, you can change the practice exercise to a reciprocal teaching exercise, where 1/5 of the class does each problem, and they come to the front of the room to explain their solution and process to the rest of the class.
If you have students that finished the classwork ahead of time, encourage them to complete Exercise 5.11: threeHeads.
BJP 5–1, Random Numbers http://media.pearsoncmg.com/aw/aw_reges_bjp_2/videoPlayer.php?id=c5-1
CS Homework Bytes, Random Numbers, with Elizabeth https://www.youtube.com/watch?v=R0MqnEofFvs
CSE 142, Random Numbers (11:12–17:17) https://www.youtube.com/watch?v=tHFmXl42-ZA&start=672
CSE 142, Guessing Game (17:18–37:13) https://www.youtube.com/watch?v=tHFmXl42-ZA&start=1038