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(System.in);
         
        // 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.
        //MyRandom.setSeed(MyCal.getTimeInMillis());
        MyRandom.setSeed(5);
         
        // 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 John@JohnMuellerBooks.com.

 

Author: John

John Mueller is a freelance author and technical editor. He has writing in his blood, having produced 99 books and over 600 articles to date. The topics range from networking to artificial intelligence and from database management to heads-down programming. Some of his current books include a Web security book, discussions of how to manage big data using data science, a Windows command -line reference, and a book that shows how to build your own custom PC. His technical editing skills have helped over more than 67 authors refine the content of their manuscripts. John has provided technical editing services to both Data Based Advisor and Coast Compute magazines. He has also contributed articles to magazines such as Software Quality Connection, DevSource, InformIT, SQL Server Professional, Visual C++ Developer, Hard Core Visual Basic, asp.netPRO, Software Test and Performance, and Visual Basic Developer. Be sure to read John’s blog at http://blog.johnmuellerbooks.com/. When John isn’t working at the computer, you can find him outside in the garden, cutting wood, or generally enjoying nature. John also likes making wine and knitting. When not occupied with anything else, he makes glycerin soap and candles, which comes in handy for gift baskets. You can reach John on the Internet at John@JohnMuellerBooks.com. John is also setting up a website at http://www.johnmuellerbooks.com/. Feel free to take a look and make suggestions on how he can improve it.