330 likes | 754 Views
Random Numbers. and simulations…. Section 6.5. Random Number Generators. Random number generators are used to simulate events. Java provides the Random class which implements a random number generator . This random number generator can produce random integers and random double numbers.
E N D
Random Numbers and simulations…. Section 6.5 Drew University
Random Number Generators • Random number generators are used to simulate events. • Java provides the Random class which implements a random number generator. • This random number generator can produce random integers and random double numbers. Drew University
class java.util.Random Drew University
Sample use: Random generator = new Random(); // Constructs the number generator. int randInt = generator.nextInt(10); // Generates a random integer from 0-9 inclusive. double randDouble = generator.nextDouble(); // Generates a random double from 0(inclusive) to 1 (exclusive). Drew University
Die class using Random import java.util.Random; public class Die { public Die(int s) { sides = s; generator = new Random(); } public int cast() { return 1 + generator.nextInt(sides); } private Random generator; private int sides; } Drew University
DieTest.java /** This program simulates casting a die ten times. */ public class DieTest { public static void main(String[] args) { Die d = new Die(6); final int TRIES = 10; for (int i = 1; i <= TRIES; i++) { int n = d.cast(); System.out.print(n + " "); } System.out.println(); } } Drew University
DieTest.java /** This program simulates casting a die ten times. */ public class DieTest { public static void main(String[] args) { Die d = new Die(6); final int TRIES = 10; for (int i = 1; i <= TRIES; i++) { int n = d.cast(); System.out.print(n + " "); } System.out.println(); } } 1 6 4 3 2 2 6 2 5 4 Drew University
Caution: • When writing programs that include random numbers, two Random objects created within the same millisecond will have the same sequence of random numbers. • If the following declarations are executed in the same millisecond, Random generator1 = new Random(); Random generator2 = new Random(); generator1 and generator2 will generate same sequence of random numbers. Drew University
Die class using Random import java.util.Random; public class Die { public Die(int s) { sides = s; generator = new Random(); } public int cast() { return 1 + generator.nextInt(sides); } private Random generator; private int sides; } Drew University
Roll two dice ten times. Record results.Consider the following first attempt! public class DieTestForTwo { public static void main(String[] args) { Die d = new Die(6); Die d2 = new Die(6); final int TRIES = 10; for (int i = 1; i <= TRIES; i++) { int n = d.cast(); int n2 = d2.cast(); System.out.println(n + " " + n2); } System.out.println(); } } Drew University
Results: 6 6 2 2 1 1 6 6 4 4 6 6 6 6 3 3 3 3 5 5 Drew University
Results: 6 6 2 2 1 1 6 6 4 4 6 6 6 6 3 3 3 3 5 5 What happened????? Drew University
What happened and why? public class DieTestForTwo { public static void main(String[] args) { Die d = new Die(6); Die d2 = new Die(6); final int TRIES = 10; for (int i = 1; i <= TRIES; i++) { int n = d.cast(); int n2 = d2.cast(); System.out.println(n + " " + n2); } System.out.println(); } } 6 6 2 2 1 1 6 6 4 4 6 6 6 6 3 3 3 3 5 5 Drew University
Another attempt: public class DieTestForTwo { public static void main(String[] args) { Die d = new Die(6); final int TRIES = 10; for (int i = 1; i <= TRIES; i++) { int n = d.cast(); int n2 = d.cast(); System.out.println(n + " " + n2); } System.out.println(); } } Drew University
New results: 2 1 1 4 3 3 5 6 6 6 4 6 2 2 5 1 3 6 2 1 Drew University
Is this the desired outcome? The difference? 2 1 1 4 3 3 5 6 6 6 4 6 2 2 5 1 3 6 2 1 public class DieTestForTwo { public static void main(String[] args) { Die d = new Die(6); final int TRIES = 10; for (int i = 1; i <= TRIES; i++) { int n = d.cast(); int n2 = d.cast(); System.out.println(n + " " + n2); } System.out.println(); } } Drew University
Problem • Write a test program for the Die class that will roll two 6-sided dice until "doubles" are rolled. Write to the screen the number of rolls it took until doubles. Drew University
A possible solution… Die d = new Die(6); int countRolls = 1; while(d.cast() != d.cast()) { countRolls++; } System.out.println(countRolls); Drew University
A possible solution with results… Die d = new Die(6); int countRolls = 1; while(d.cast() != d.cast()) { countRolls++; } System.out.println(countRolls); 5 Drew University
Suppose I want to "see" the rolls? Drew University
Suppose I want to "see" the rolls? Die d = new Die(6); int countRolls = 1; while(d.cast() != d.cast()) { countRolls++; System.out.println(d.cast() + " " + d.cast()) } System.out.println(countRolls); Drew University
Suppose I want to "see" the rolls? Die d = new Die(6); int countRolls = 1; while(d.cast() != d.cast()) { countRolls++; System.out.println(d.cast() + " " + d.cast()) } System.out.println(countRolls); Doubles 3 6 4 6 3 3 5 6 5 Drew University
Suppose I want to "see" the rolls? Die d = new Die(6); int countRolls = 1; while(d.cast() != d.cast()) { countRolls++; System.out.println(d.cast() + " " + d.cast()) } System.out.println(countRolls); Doubles 3 6 4 6 3 3 5 6 5 What happened? Drew University
A solution: Die d = new Die(6); int countRolls = 1; int roll1=d.cast(); int roll2=d.cast(); System.out.println(roll1 + " " + roll2); while(roll1 != roll2) { countRolls++; int roll1=d.cast(); int roll2=d.cast(); System.out.println(roll1 + " " + roll2); } System.out.println(countRolls); Drew University
A solution with results: Die d = new Die(6); int countRolls = 1; int roll1=d.cast(); int roll2=d.cast(); System.out.println(roll1 + " " + roll2); while(roll1 != roll2) { countRolls++; roll1=d.cast(); roll2=d.cast(); System.out.println(roll1 + " " + roll2); } System.out.println(countRolls); 6 5 4 2 4 5 5 6 3 3 5 Drew University
Another choice for a loop? d = new Die(6); countRolls = 0; do { countRolls++; roll1=d.cast(); roll2=d.cast(); System.out.println(roll1 + " " + roll2); } while(roll1 != roll2); System.out.println(countRolls); 1 4 1 6 6 2 6 1 4 4 5 Drew University
Remember…. • It is a better idea to share a single random number generator in the entire program. • We rarely want to generate identical sequences of random numbers! Drew University