1 / 27

Random Numbers

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.

minty
Download Presentation

Random Numbers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Random Numbers and simulations…. Section 6.5 Drew University

  2. 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

  3. class java.util.Random Drew University

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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. Results: 6 6 2 2 1 1 6 6 4 4 6 6 6 6 3 3 3 3 5 5 Drew University

  12. 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

  13. 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

  14. 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

  15. New results: 2 1 1 4 3 3 5 6 6 6 4 6 2 2 5 1 3 6 2 1 Drew University

  16. 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

  17. 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

  18. A possible solution… Die d = new Die(6); int countRolls = 1; while(d.cast() != d.cast()) { countRolls++; } System.out.println(countRolls); Drew University

  19. 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

  20. Suppose I want to "see" the rolls? Drew University

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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

More Related