Topics Random numbers Random number generators Uniform and non-uniform distributions The Random...

62

Transcript of Topics Random numbers Random number generators Uniform and non-uniform distributions The Random...

Page 1: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.
Page 2: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

TopicsTopics

•Random numbers•Random number generators•Uniform and non-uniform distributions•The Random class•Seeds for Random()•Tossing dice•Picking passwords

Page 3: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Programs frequently need random numbers. A card game uses random numbers to create a shuffled deck of cards. A dice game uses random numbers to simulate a throw. Many activities in the real world (such as traffic on a highway system) appear to be random, and a program that models them must use random numbers. This chapter discusses the Random class that comes with Java.

Random NumbersRandom Numbers

Page 4: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Random numbers, probability, and statistics are important for testing, for modeling the world, and for general understanding of the universe. Computer science degree programs usually require a course in these subjects.

Random NumbersRandom Numbers

Page 5: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Question #1Question #1

What number is most likely outcome for a throw of a fair die?

Page 6: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Random NumbersRandom NumbersA single die can be regarded as a random number generator that selects a number from the range 1 through 6, and each number is equally likely. The same is true for a second throw of the die. The outcome of the first throw does not affect the outcome of the second throw (or of any other throw).

If you throw a die many times, you will get a sequence of random numbers. Since each number is equally likely, you would expect each number to occur about as often as any other number. For example, if you threw the die 60 times, you would expect to see about 10 of each number. However, it would be unusual to see exactly 10 of each number. The die does not "remember" its previous outcomes and does nothing to catch up with numbers they may have fallen short.

Page 7: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

QUESTION 2:QUESTION 2:

A game spinner (say) has 12 divisions, numbered 1 through 12. Which number is most likely?

Page 8: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Uniform DistributionUniform DistributionWhen each number in a range of numbers is equally likely, the numbers are uniformly distributed. Many random number generators, such as a single die or a spinner, produce a uniform distribution.

However, some random number generators are not uniform. For example, when two dice are thrown and the outcome is the sum of the spots, the range of possible outcomes is 2 through 12, but 7 is the most likely outcome since there are 6 combinations that sum to 7, more than any other number. Six and 8 are the second most likely outcomes, since there are 5 combinations each that sum to those numbers.

Page 9: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Question 3Question 3What are the least likely outcomes from a pair of dice?

(Hint: study the table.)

Page 10: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

The Random ClassThe Random ClassThe Random class can be used in programs that need random numbers. Random is part of the java.util package. The numbers that Random generates are actually pseudorandom, because they are calculated using a formula. But they appear to be random and can be used in most situations that call for random numbers.

A Random object is started out with a seed value which determines the sequence of numbers it will produce. Every Random object started with the same seed value will produce the same sequence of numbers. There are two constructors for Random objects:

Page 11: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

The Random ClassThe Random ClassRandom() — Creates a new random number generator using a seed based on the current time.

Random( long seed ) — Creates a new random number generator based on a specific seed value.

The first constructor uses a seed value based on the current time in milliseconds. Use it when you need a different stream of random numbers for each run of the program. For example, a game program that uses random numbers to simulate a shuffled deck of cards needs a different stream each time the game is played.

The second constructor gives you the ability to repeat a pseudorandom sequence. Use it when you need to test different strategies against the same stream of random events.

Page 12: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

The Random ClassThe Random Class

Here is an example of constructing a random number generator:

Random rand = new Random();

Now the methods of the Random class can be used with rand.

Page 13: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Question #4Question #4

Are there situations where you do want to use the exact same "random" sequence

for several runs of a program?

Page 14: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

nextInt()nextInt()Here are two methods (out of several) that Random objects provide. The second method is the one most often used.

num must be a positive integer. The random integers are selected from the range 0 up to and including num-1 but not including num.

int nextInt() — Returns the next pseudorandom, uniformly distributed int value.

All possible int values, both positive and negative, are in the range of values returned.

int nextInt( int num ) — Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and num (exclusive).

Page 15: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Question #5Question #5

Say that you want pseudorandom numbers selected from the range 1 through 6.

How can nextInt( int num ) be used for that?

Page 16: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Die Toss ProgramDie Toss ProgramThe trick is to determine how many integers are in the range of outcomes you want, and use that as the parameter to nextInt(). Then add the minimum value of the range you want.

In this example we want 1,2,3,4,5,6 so the number of outcomes is 6. nextInt(6) will output numbers from 0,1,2,3,4,5 so add one to get the desired range.

Here is a program that simulates tossing a die.

import java.util.Random; import java.util.Scanner; public class DieToss { public static void main ( String[] args ) { Scanner scan = new Scanner( System.in ); Random rand = new Random( ); while ( true ) { System.out.print("You toss a " + (rand.nextInt(6)+1) ); String line = scan.nextLine(); } } }

Page 17: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Die Toss ProgramDie Toss ProgramThe statement:

Random rand = new Random();

creates a random number generator using a seed based on the current time. The program will produce a different sequence of numbers each time it is run.

The while loop runs until the user hits control-C (or otherwise kills the program). The loop body selects a random number from the range 1 through 6 and prints it out:

System.out.print("You toss a " + (rand.nextInt(6)+1) );

Then the loop body waits until the user hits "enter". (Actually, any line ending with "enter" would work.)

String line = scan.nextLine();

Nothing is done with the line the user types; it is just used to pause the output. (You could improve the program by having it end gracefully when the user types "Q" or "q".)

Page 18: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Question #6Question #6

Say that you want random integers in The range -5 to +5 (inclusive).

How can you use nextInt() for that? (Hint: see the previous answer.)

Page 19: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Example OutputExample OutputHere is a run of the die-tossing program:

K:\cai\>java DieToss You toss a 2 You toss a 1 You toss a 4 You toss a 4 You toss a 6 You toss a 5 You toss a 3 You toss a 2Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Unknown Source) at DieToss.main(DieToss.java:15)

The user hit control-C to end the program.

Page 20: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Question #7Question #7

(Thought Question: )

You want to simulate the toss of two dice.

Do you need two random number generators?

Page 21: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Two-dice Gotcha!Two-dice Gotcha!In fact, if you use two random number generators, you need to be careful. The following code:

Random rand1 = new Random(); Random rand2 = new Random();

will most likely produce two random number generators which are initialized to the same seed. Each generator would then produce the same pseudorandom sequence. This is not what you want.

The reason this happens is because the constructor Random() creates a seed based on the current time in milliseconds. Time changes little between the execution of the two statements, so both random number generators use the same seed. Two generators started with the same seed generate the same sequence of pseudorandom numbers. You could initialize the second random number generator using a random number from the first, but it is more convenient to use just one random number generator.

Page 22: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Question #8Question #8

Do you ever need more than one random number generator?

Page 23: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Two-dice ProgramTwo-dice ProgramHere is a program that simulates the toss of two dice:

import java.util.*; public class TwoDieToss { public static void main ( String[] args ) { Scanner scan = new Scanner( System.in ); Random rand = new Random();

while ( true ) { System.out.print("You toss a " + (rand.nextInt(6)+1 + rand.nextInt(6)+1) ); String input = scan.nextLine(); } } }

Page 24: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Two-dice ProgramTwo-dice ProgramThe two tosses and the sum of the spots is implemented in the expression:

(rand.nextInt(6)+1 + rand.nextInt(6)+1)

Each call to nextInt(6) is completely independent of the previous call, so this is the same as throwing two independent dice.

Throwing one die twice and adding up each outcome is equivalent to throwing two dice and adding up each die. However, throwing a 12-sided die once is not equivalent to throwing two 6-siced dice.

Page 25: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Question #9Question #9

Would the following work to simulate two dice:

(rand.nextInt(11)+2)

Page 26: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Example OutputExample OutputHere is a sample run of the above (correct) program:

You toss a 11 You toss a 8 You toss a 6 You toss a 5 You toss a 5 You toss a 5 You toss a 6 You toss a 6 You toss a 6 You toss a 12 You toss a 4 You toss a 5 Exception in thread "main" . . .

Page 27: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Example OutputExample OutputHere is a sample run of a program that (incorrectly) uses:

(rand.nextInt(11)+2)

You toss a 6 You toss a 10 You toss a 11 You toss a 4 You toss a 6 You toss a 5 You toss a 11 You toss a 9 You toss a 7 You toss a 2 You toss a 4 You toss a 3 Exception in thread "main" . . .

Page 28: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Example OutputExample OutputWith such a small sample, you can't tell for sure that something is wrong, but you might be suspicious. To confirm your suspicions, gather much more data, then count the number of times each outcome occurs and check that the results match what you expect. This is part of what is studied in a probability and statistics course.

Page 29: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Question #10Question #10Would you ever need a random floating point value?

Page 30: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Floating Point Random MethodsFloating Point Random Methods

double nextGaussian() — Returns a pseudorandom, double-precision value drawn from a Gaussian distribution (also called a "normal" distribution or a "bell-shaped curve".) The mean of the distribution is 0.0 and the standard deviation is 1.0.

float nextFloat() — Returns a pseudorandom, floating point value in the range 0.0 up to but not including 1.0.

double nextDouble() — Returns a pseudorandom, double-precision value in the range 0.0 up to but not including 1.0.

Here are some more methods of the Random class:

Page 31: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Floating Point Random MethodsFloating Point Random MethodsNotice that nextFloat() and nextDouble() output random floating point values in the range [0.0, 1.0), all values starting at zero, up to but not including 1.0.

You might think that it is strange not to include 1.0, but this is actually convenient, and is what is done by nearly all floating point random number generators.

To visualize a random floating point generator, think of a spinner centered on a circle with a circumference of one foot. To generate a random float, spin the pointer, then measure the distance along the circle, from the zero mark. Measure the distance using "hundredths of a foot". The distance is the randomly selected float in hundredths.

If the circumference is exactly one foot, then the "0.0" mark on the circle and the "1.0" mark on the circle would be the same. This might be awkward. But 1.0 is excluded from the range so the problem is avoided.

Page 32: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Question #11Question #11

Say that you want random doubles in the range 0.0 up to (but not including) 10.0.

How can you get that range using nextDouble()?

Page 33: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Calculator TesterCalculator Tester

Say that you are not convinced that Java and your electronic calculator give the same results for sin(x). You could test this by checking the values each produces for some standard angles, such as 0.0, pi/2 and so on.

But you should also test several randomly selected angles. Here is a program that calculates sin(x) for random angles (in radians) between -10*pi and +10*pi.

Page 34: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Calculator TesterCalculator Testerimport java.util.Random; class SineTester { public static void main ( String[] args ) { int j=0; Random rand = new Random(); System.out.println(" x " + "\t\t\t sin(x)"); while ( j<10 ) { double x = rand.nextDouble()*(20*Math.PI) - 10*Math.PI; System.out.println("" + x + "\t" + Math.sin(x)); j = j+1; } } }

Page 35: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Calculator TesterCalculator TesterRandom values are often used for testing. Sometimes things work perfectly for standard values, but not for random ones. For example, some gas stations in California adjusted their pumps to dispense exactly the correct amount of fuel for multiples a half gallon, but to dispense less fuel for all other amounts.

The Bureau of Weights and Measures tested the pumps at standard volumes (half a gallon, one gallon, ten gallons and such) so the pumps passed inspection. Testing random volumes would have revealed the scam.

Page 36: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Question #12Question #12

Might you ever need to simulate flipping a coin?

Page 37: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

More Random MethodsMore Random MethodsHere are some more methods of the Random class:

long nextLong() — Returns a pseudorandom, uniformly distributed long value.

All possible long values, both positive and negative, are in the range of values returned.

boolean nextBoolean() — Returns a pseudorandom boolean value.

void setSeed(long seed) — sets the seed of the random number generator.

This might be done in several places in a program so that different objects are tested against the same random events.

There are some other methods of Random not described here.

Page 38: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Question #13Question #13Think of a really great secret password

for your computer account.

Page 39: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Sounds like you need to consult Nigerian Security Services, Ltd. Here is their program for generating random passwords:

import java.util.*; class PasswordGenerator { public static void main ( String[] args ) { Scanner scan = new Scanner( System.in ); Random rand = new Random(); int digits = 0;

while ( digits < 5 ) { System.out.println("Your password must have at least 5 characters."); System.out.print("How many characters do you want in your password? "); digits = scan.nextInt(); }

String choices = "abcdefghijklmnopqrstuvwxyz" ; choices = choices + choices.toUpperCase() ; choices = choices + "1234567890" ; String password = ""; int j = 0;

while ( j<digits ) { password = password + choices.charAt( rand.nextInt( choices.length() ) ); j = j + 1; } System.out.println("Here is your password: " + password ); } }

Page 40: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Password ProgramPassword ProgramHere are a few runs of the program:

K:\>java PasswordGenerator Your password must have at least 5 characters. How many characters do you want in your password? 4 Your password must have at least 5 characters. How many characters do you want in your password? 3 Your password must have at least 5 characters. How many characters do you want in your password? 8 Here is your password: BaXpmUsA

K:\>java PasswordGenerator Your password must have at leas 5 characters. How many characters do you want in your password? 12 Here is your password: ly3YFAhM8HDH

The details of this program are explained in the next few pages.

Page 41: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Question #14Question #14

What is the purpose of the following loop from the program:

while ( digits < 5 ) { System.out.println("Your password must have at least 5 characters."); System.out.print("How many characters do you want in your password? "); digits = scan.nextInt(); }

Page 42: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

String of Characters String of Characters Next, the program assembles a string of all the characters that could go into a password:

String choices = "abcdefghijklmnopqrstuvwxyz”; choices = choices + choices.toUpperCase(); choices = choices + "1234567890" ;

The first of these three statements creates a string literal containing the lower case alphabet. Then the expression choices.toUpperCase() creates a string that is all upper case, based on the string currently referred to by choices. This string is then concatenated onto the original choices string:

choices = choices + choices.toUpperCase();

Recall that + means "string concatenation". Finally, the string "1234567890" is concatenated onto the string to produce the final string, shown below:

"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"

Page 43: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Question #15Question #15

Would the statement:

choices = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";

have worked as well?

Page 44: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Building the Password Building the Password Next, the program builds the password:

String password = ""; int j = 0;

while ( j<digits ) { password = password + choices.charAt( rand.nextInt( choices.length() ) ); j = j + 1; }

Page 45: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Building the Password Building the Password

First the password is initialized to the empty string. Then random characters, selected from choices, are appended to it, one-by-one. The expression:

rand.nextInt( choices.length() )

randomly picks an integer from 0 up to (but not including) the length of the choices string. Then that integer is used to select a character from the choices string.

Say that the random integer were 7. Then choices.charAt( 7 )

is character number 7 in the string choices, where counting starts at zero. This character is appended to the password and the loop repeats. (For more about string methods, see chapter 29.)

Page 46: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Question #16Question #16

Examine this statement:

password = password + choices.charAt( rand.nextInt( choices.length() ) );

Four things happen in that one statement. This can be hard to keep track of. Is the following code fragment equivalent?

int range = choices.length(); int characterNumber = rand.nextInt( range ); char ch = choices.charAt( characterNumber ); password = password + ch;

Page 47: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Dice GameDice GameHere are the rules to a dice game:

1.The game consists of several "rounds."

2.In each round, the computer plays first by throwing two six-sided dice and adding the spots.

3.The player must now try to beat that sum.

4.The player may choose to throw two six-sided dice or one 11-sided die (with sides numbered 2-12).

5.If the player's throw beats the computer, then the player wins the round. Otherwise the computer wins the round.

6.The game continues until either the player or the computer has won ten rounds.

The player can chose to throw two dice, which randomly select a number 2 through 12, or to throw the 11-sided die, which also randomly selects a number 2 through 12. But the distribution of numbers is different for each choice.

Page 48: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Dice GameDice GameHere are the first four rounds of a game:

The Computer tosses: 10 Toss 1 eleven-sided die, or 2 six-sided dice (enter 1 or 2)? 1 You throw 1 die and get: 7 You loose the round! Score: computer 1, you 0

The Computer tosses: 4 Toss 1 eleven-sided die, or 2 six-sided dice (enter 1 or 2)? 2 You throw 2 dice and get: 9 You win the round! Score: computer 1, you 1 The Computer tosses: 10

Toss 1 eleven-sided die, or 2 six-sided dice (enter 1 or 2)? 1 You throw 1 die and get: 3 You loose the round! Score: computer 2, you 1

The Computer tosses: 5 Toss 1 eleven-sided die, or 2 six-sided dice (enter 1 or 2)? 2 You throw 2 dice and get: 5 You loose the round! Score: computer 3, you 1

Page 49: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Dice GameDice Game

Eleven-sided dice are hard to find in the real world. If you want to play this game with physical dice use a twelve-sided or sixteen-sided die (which do exist, ask any Dungeon Master) and discard rolls other than 2 through 12.

Page 50: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Question #17Question #17

What advantage does the computer have in this game?

What advantage does the player have in this game?

Page 51: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

What advantage does the computer have in this game?

The computer's throw must be beaten by the player. In case of a tie, the computer wins the round.

What advantage does the player have in this game?

The player can select from a uniform distribution (one 11-sided die) or a non-uniform distribution (two regular dice) with 6-7-8 as the most common outcomes. Use the uniform distribution when a high number is needed; use the non-uniform distribution when 6-7-8 is enough to win a round. Is this enough to overcome the computer's advantage? Play the game and see!

Page 52: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Flow ChartFlow Chart

Here is the main logic of the program. Study how it implements the rules of the game. Some additional details are needed for some of the steps.

The "Player's Toss" box, for one, needs to implement the details of giving the player a choice of dice and then throwing that choice.

Page 53: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Question #18Question #18

How many random number generators are needed for this program?

Page 54: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Complete ProgramComplete ProgramHere is the complete program. Its sections will be explained shortly. The sections of the program match the boxes in the flowchart. You can copy and paste this program to a file and play with it, if you want.

import java.util.*; class DiceChoiceGame { public static void main ( String[] args ) { // Declare and Initialize final int endScore=10; Scanner scan = new Scanner( System.in ); Random rand = new Random(); int playerScore=0, compScore=0; int playerToss, compToss; // Play Rounds until one player reaches ending score while ( playerScore<endScore && compScore<endScore ) { // Computer's Toss compToss = rand.nextInt(6)+1 + rand.nextInt(6)+1 ; System.out.println("The Computer tosses: " + compToss); // Player's Toss System.out.print("Toss 1 eleven-sided die, or 2 six-sided dice (enter 1 or 2)? "); String numDice = scan.nextLine(); if ( numDice.equals("1") ) { playerToss = rand.nextInt(11)+2 ; System.out.println("You throw 1 die and get: " + playerToss ); } else { playerToss = rand.nextInt(6)+1 + rand.nextInt(6)+1 ; System.out.println("You throw 2 dice and get: " + playerToss ); } // Determine Winner of Round and Adjust Scores if ( playerToss>compToss ) { playerScore = playerScore+1; System.out.println("You win the round!"); } else { compScore = compScore+1; System.out.println("You loose the round!"); } System.out.println("Score: computer " + compScore + ", you " + playerScore + "\n"); } // Determine Winner of the Game System.out.println("Final Score: computer " + compScore + ", you " + playerScore); if ( compScore > playerScore ) System.out.println("The Computer Wins!"); else System.out.println("You Win the Game!"); } }

Page 55: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Question #19Question #19

Will one player or the other eventually win?

Or is a draw possible?

Page 56: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Main LoopMain LoopHere is the logic that implements the main loop:

// Declare and Initialize final int endScore=10; int playerScore=0, compScore=0;

// Play Rounds until one player reaches ending score while ( playerScore<endScore && compScore<endScore ) { . . . . }

The body of the loop corresponds to one round of the game.

Page 57: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Question #20Question #20

(Review: ) What does final int endScore=10; do?

Page 58: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

The Tosses The Tosses

The computer's toss is straightforward. This is just summing up the toss of two individual six-sided dice.

// Computer's Toss compToss = rand.nextInt(6)+1 + rand.nextInt(6)+1 ; System.out.println("The Computer tosses: " + compToss);

For the user's toss, first the program asks the user which dice to use:

// Player's Toss System.out.print("Toss 1 eleven-sided die, or 2 six-sided dice (enter 1 or 2)? "); String numDice = scan.nextLine();

Page 59: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

The Tosses The Tosses No numerical use is made of the digit that the user types, so it is best to read it in as a string. That way if the user accidentally hits a non-digit the program will not crash. String comparison is then used to determine which option to take. Everything but digit "1" will result in the two-dice branch.

if ( numDice.equals("1") ) { playerToss = rand.nextInt(11)+2 ; System.out.println("You throw 1 die and get: " + playerToss ); } else { playerToss = rand.nextInt(6)+1 + rand.nextInt(6)+1 ; System.out.println("You throw 2 dice and get: " + playerToss );}

Page 60: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Question #21Question #21

Wouldn't it be better to test if the user types exactly the specified choices?

Page 61: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Determining the Winner of the Determining the Winner of the RoundRound

The final code in the loop body determines the winner of the round:

There is nothing difficult here, but notice that the code is written so that the computer wins when the dice tosses are equal.

// Determine Winner of Round and Adjust Scores if ( playerToss > compToss ) { playerScore = playerScore+1; System.out.println("You win the round!"); } else { compScore = compScore+1; System.out.println("You loose the round!"); } System.out.println("Score: computer " + compScore + ", you " + playerScore + "\n");

Page 62: Topics Random numbers Random number generators Uniform and non-uniform distributions The Random class Seeds for Random() Tossing dice Picking passwords.

Question #22Question #22

As another exercise, you could add code to do something else when the tosses are equal.