//******************************************************************** // RandomNumbers.java Author: Lewis and Loftus // // Demonstrates the import statement, and the creation of pseudo- // random numbers using the Random class. //******************************************************************** import java.util.Random; public class RandomNumbers { //----------------------------------------------------------------- // Generates random numbers in various ranges. //----------------------------------------------------------------- public static void main (String[] args) { Random generator = new Random(); int num1; float num2; num1 = generator.nextInt(); System.out.println ("A random integer: " + num1); num1 = Math.abs (generator.nextInt()) % 10; System.out.println ("0 to 9: " + num1); num1 = Math.abs (generator.nextInt()) % 10 + 1; System.out.println ("1 to 10: " + num1); num1 = Math.abs (generator.nextInt()) % 20 + 10; System.out.println ("10 to 29: " + num1); num2 = generator.nextFloat(); System.out.println ("A random float [between 0-1]: " + num2); num2 = generator.nextFloat() * 6; // 0.0 to 5.999999 num1 = (int) num2 + 1; System.out.println ("1 to 6: " + num1); } }