Conditional and Random Behavior


Review


 
Example: The code
        Random generator = new Random();
        int randNum = generator.nextInt(10);
creates a random number generator and uses it to generate one of the 10 numbers between 0 and 9 (the parameter specifies the number of possible values). If we call nextInt(10) repeatedly, we'll get a sequence of pseudo-random numbers in that range, such as:
        8, 5, 0, 0, 4, 3, 1, 3, 1, 0, 3, 6, 7, 0, 4, 4, 3, 9, 2, 8, ...

The Random class is in the java.util package.


Think — Pair — Share

Consider the following program:

Example: Choosing Randomly

Random generator = new Random();
int randNum = generator.nextInt(100);
if ( randNum < 4 )
    student.setFirstDestination("Volunteer / Service Opportunity");
else if ( randNum < 23 )
    student.setFirstDestination("Continuing Education");
else
{
    randNum = generator.nextInt(100);
    if ( randNum < 91 )
        student.setFirstDestination("Employed");
    else
        student.setFirstDestination("Unknown");
} 
  

Alyce Brady, Kalamazoo College