13-ch05-2-random
13-ch05-2-random
Chapter 5
Lecture 5-2: Random Numbers
1
http://xkcd.com/221/
2
Randomness
Lack of predictability: don't know what's coming next
4
The Random class
A Random object generates pseudo-random numbers.
Class Random is found in the java.util package.
import java.util.*;
Method name Description
nextInt() returns a random integer
nextInt(max) returns a random integer in the range [0, max)
in other words, 0 to max-1 inclusive
nextDouble() returns a random real number in the range [0.0, 1.0)
Example:
5
Generating random numbers
Common usage: to get a random number from 1 to N
int n = rand.nextInt(7) + 4;
6
Random questions
Given the following declaration, how would you get:
Random rand = new Random();
7
Random and other types
nextDouble method returns a double between 0.0 - 1.0
Example: Get a random GPA value between 1.5 and 4.0:
double randomGpa = rand.nextDouble() * 2.5 + 1.5;
int r = rand.nextInt(3);
if (r == 0) {
System.out.println("Rock");
} else if (r == 1) {
System.out.println("Paper");
} else { // r == 2
System.out.println("Scissors");
}
8
Random question
Write a program that simulates rolling of two 6-sided dice
until their combined result comes up as 7.
2 + 4 = 6
3 + 5 = 8
5 + 6 = 11
1 + 1 = 2
4 + 3 = 7
You won after 5 tries!
9
Random answer
// Rolls two dice until a sum of 7 is reached.
import java.util.*;
public class Dice {
public static void main(String[] args) {
Random rand = new Random();
int tries = 0;
int sum = 0;
while (sum != 7) {
// roll the dice once
int roll1 = rand.nextInt(6) + 1;
int roll2 = rand.nextInt(6) + 1;
sum = roll1 + roll2;
System.out.println(roll1 + " + " + roll2 + " = " + sum);
tries++;
}
System.out.println("You won after " + tries + " tries!");
}
}
10
Point objects
Java has a class of objects named Point.
They store two values, an (x, y) pair, in a single variable.
They have useful methods we can call in our programs.
To use Point, you must write:
import java.awt.*;
11
Point data and methods
Data stored in each Point object:
Field name Description
x the point's x-coordinate
y the point's y-coordinate
12
Random drawing question
Write a program that draws 100x100 rectangles at random
(x, y) positions within a 500x500 DrawingPanel.
Print how far away the upper left corner of the rectangle is
from the middle of the screen
13
Random drawing question
Modify the rectangle program to draw randomly placed/
colored 10x10 rectangles until it draws 20 red ones.
Break up your program using static methods.
Print a line of output each time a red rectangle is drawn:
4 + 10 + 3 + 10 = 27
9 + 2 = 11
8 + 6 + 7 + 9 = 25
Wrong! The answer was 30
5 + 9 = 13
Wrong! The answer was 14
4 + 9 + 9 = 22
3 + 1 + 7 + 2 = 13
4 + 2 + 10 + 9 + 7 = 42
Wrong! The answer was 32
You earned 4 total points.
15
Random answer
// Asks the user to do adding problems and scores them.
import java.util.*;
16
Random answer 2
...
// Builds one addition problem and presents it to the user.
// Returns 1 point if you get it right, 0 if wrong.
public static int play(Scanner console, Random rand) {
// print the operands being added, and sum them
int operands = rand.nextInt(4) + 2;
int sum = rand.nextInt(10) + 1;
System.out.print(sum);
for (int i = 2; i <= operands; i++) {
int n = rand.nextInt(10) + 1;
sum += n;
System.out.print(" + " + n);
}
System.out.print(" = ");
// read user's guess and report whether it was correct
int guess = console.nextInt();
if (guess == sum) {
return 1;
} else {
System.out.println("Wrong! The answer was " + total);
return 0;
}
}
} 17