Practice Icon on Page 59

A number of you have written to ask me about the Practice icon on page 59 of Java eLearning Kit for Dummies. It turns out that the practice won’t quite work as originally written because Java’s Random class now ensures that you get a random number each time you call it. As a result, the original practice no longer works.

In order to obtain consistent results from the example, you would need to set the seed to the same value every time. Here is an example of what I mean:

// Import the required API classes.
import java.util.Scanner;
import java.util.Random;
import java.util.Calendar;
public class ShowInt
    public static void main(String args[])
        // Create the scanner.
        Scanner GetInt = new Scanner(;
        // Obtain an int value.
        System.out.print("Type a number between 1 and 10: ");
        int YourGuess = GetInt.nextInt();
        // Get the current time.
        Calendar MyCal = Calendar.getInstance();
        // Create a random number generator.
        Random MyRandom = new Random();
        // Set the seed value for the random number using
        // the current number of milliseconds in the time.
        // Obtain a random number between 1 and 10.
        int MyGuess = MyRandom.nextInt(10) + 1;
        // Display the value on screen.
        System.out.print("Your guess was: " + YourGuess);
        System.out.println(" My guess was: " + MyGuess);

Notice that I’ve commented out the original MyRandom.setSeed(MyCal.getTimeInMillis()); line that ensures you get a more random result every time and added a new MyRandom.setSeed(5); line that sets the seed to the same value every time. Now when you run the example, you get the same value every time. On my system, the example guesses 8 every time.

This change to the practice should produce the desired result. I’m sorry about any confusion the original practice may have causes. Please let me know about any other book-related concerns at