Random Class
An instance of the Random can be used to generate a stream of
random values
Typical process:
1. Create a Random object
2. Use the object to get random values. Use one of:
nextInt() returns a random integer
nextInt(max) returns an integer random value in [0, … max )
nextDouble() returns a random value in [0.0, 1.0 )
nextBoolean() returns a random value from {true, false}
Note: need an import statement
import java.util.Random;
1
Random Class
Examples
1. Simulate a 3-way choice: rock/paper/scissors – use nextInt(3)
2. Simulate coin toss – use nextInt(2)
3. Simulate coin toss – use nextBoolean()
4. Simulate tossing a coin 100 times counting number of heads
2
Random class – Rock/paper/scissors
Example 1. code to display a random Rock-Paper-Scissors:
// three outcomes, all equally likely
Random rand = new Random();
// nextInt(3) produces a value from {0, 1, 2}
switch( rand.nextInt(3) ) {
case 0:
System.out.println("Rock");
break;
case 1:
System.out.println("Paper");
break;
case 2:
System.out.println("Scissors");
break;
}
3
Random class – toss a coin
Example 2. code to display a random coin toss:
// two-sided coin: heads/tails
Random rand = new Random();
// nextInt(2) produces values from {0, 1}
switch( rand.nextInt(2) ) {
case 0:
System.out.println("Heads");
break;
case 1:
System.out.println("Tails");
break;}
Tossing coins using Booleans next slide
4
Random class – toss a coin using booleans
Example 3. code to display a random coin toss:
// two-sided coin: heads/tails
Random rand = new Random();
// nextBoolean() produces values from {true, false}
// cannot switch on booleans use if-else
if( rand.nextBoolean() )
System.out.println("Heads");
else
System.out.println("Tails");
5
Random class
Example 4: Simulate tossing a coin 100 times (page 117 in text)
public class TossCoin
{
public static void main ( String [] args )
{
int heads = 0; // counter for heads
System.out.print("\ n100 tosses : ");
Random g = new Random () ;
for (int i=0; i<100; i++)
if( g.nextBoolean() ) heads ++;
System.out.println("\ nHeads : "+ heads
+"\ nTails : "+(100 - heads ) ) ;
}
}
6
Random Number Generators
ASIDE: What does a random number generator look like?
https://en.wikipedia.org/wiki/Linear_congruential_generator
nextValue (a * previousValue + c) mod m
Java: next (25214903917*previous + 11) mod 231
uses 48-bit values at each iteration but returns
the 32 most significant bits
7
Character class
An instance of the Character class is not required
Character contains many useful utility methods
8
Character class
Examples
Notice in the code how the Character class methods are specified
when there is no object … this may seem odd, but …
1. Detecting letters, digits
2. Getting a numeric value of a character that is a digit
3. Checking a control number for validity
• Suppose all characters must be numeric
• Consider exercise 6 on page 125
9
Character class
Example 1
public class CharacterTypes
A line of text is examined, character-by-character, to determine the
character’s type where type is one of {letter, digit, other}
Character methods used:
isLetter(…) returns true if the character is a letter
isDigit(…) returns true if the character is a digit
No instance of Character is used which means the methods are called
using statements of the form
If ( Character.isDigit(c) ) System.out.println(…
Prefix Character. Is needed The argument passed to isDigit is the character c
to reference a static method
of the Character class The method to execute is isDigit
10