Random Numbers

27
Fran Trees Drew University 1 Random Numbers and simulations…. Section 6.5

description

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. - PowerPoint PPT Presentation

Transcript of Random Numbers

Page 1: Random Numbers

Fran Trees Drew University 1

Random Numbers

and simulations….

Section 6.5

Page 2: Random Numbers

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

Page 3: Random Numbers

Fran Trees Drew University 3

class java.util.Random

Method Method Summary

int nextInt(n) Returns a random integer in the range from 0 to n-1 inclusive.

double nextDouble() Returns a random floating-point number between 0 (inclusive) and 1 (exclusive).

Page 4: Random Numbers

Fran Trees 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).

Page 5: Random Numbers

Fran Trees Drew University 5

Die class using Randomimport 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;}

Page 6: Random Numbers

Fran Trees 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(); }}

Page 7: Random Numbers

Fran Trees 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

Page 8: Random Numbers

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

Page 9: Random Numbers

Fran Trees Drew University 9

Die class using Randomimport 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;}

Page 10: Random Numbers

Fran Trees 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(); }}

Page 11: Random Numbers

Fran Trees Drew University 11

Results:

6 6

2 2

1 1

6 6

4 4

6 6

6 6

3 3

3 3

5 5

Page 12: Random Numbers

Fran Trees 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?????

Page 13: Random Numbers

Fran Trees Drew University 13

What happened and why?

6 6

2 2

1 1

6 6

4 4

6 6

6 6

3 3

3 3

5 5

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(); }}

Page 14: Random Numbers

Fran Trees 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(); }}

Page 15: Random Numbers

Fran Trees 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

Page 16: Random Numbers

Fran Trees 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(); }}

Page 17: Random Numbers

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

Page 18: Random Numbers

Fran Trees Drew University 18

A possible solution…

Die d = new Die(6);

int countRolls = 1;

while(d.cast() != d.cast())

{

countRolls++;

}

System.out.println(countRolls);

Page 19: Random Numbers

Fran Trees 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

Page 20: Random Numbers

Fran Trees Drew University 20

Suppose I want to "see" the rolls?

Page 21: Random Numbers

Fran Trees 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);

Page 22: Random Numbers

Fran Trees 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);

Doubles3 64 63 35 65

Page 23: Random Numbers

Fran Trees 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);

Doubles3 64 63 35 65

What happened?

Page 24: Random Numbers

Fran Trees 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);

Page 25: Random Numbers

Fran Trees 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 54 24 55 63 35

Page 26: Random Numbers

Fran Trees 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 41 66 26 14 45

Page 27: Random Numbers

Fran Trees 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!