We are going to learn how to use the Random class to generate a random number and also develop a complete lotto ticket generator program. You can use this program to generate your own random lotto ticket numbers.

Generate a random number using the Random class

The nextInt(int bound) function of the Random class generate a random integer number between 0 (inclusive) and bound (exclusive). For example, the following Java code generates and prints out a random number between 0 (inclusive) and 10 (exclusive).

int number;
Random random = new Random();
number = random.nextInt(10);
System.out.println(number);

For more information on the Random class, click on here.

Generate a lotto row

The function below generates and returns a lotto row.

/*
 * Function description:
 *  	Generate and return a lotto row
 * 
 * Parameters: 
 * 	n: 	How many numbers in a lotto row
 *  	max: 	The highest number can be drawn
*/
private String lottoRow(int n, int max) {
    Random random = new Random();
    List<Integer> row = new ArrayList<Integer>(n);
    String rowStr = "";
    List<Integer> list = new ArrayList<Integer>(max);
    
    
    // Have a list of 1 .. max
    for (int i = 1; i < (max + 1); i++) {
    	list.add(i);
    }
    
    // Create a row of n random numbers from the list above
    for (int i = 0; i < n; i++) {
    	row.add(list.remove(random.nextInt(max - i)));
    }
    
    // Sort ascending
    Collections.sort(row);
    
    // Generate a lotto line
    for (int i = 0; i < n; i++) {
    	rowStr += row.get(i) + " ";
    }
    
    return rowStr;
}

Generate a lotto power ball number

The function draws and returns a power ball number.

/*
 * Function description:
 *  	Draws and returns a power ball number
 * 
 * Parameters: 
 *  	max: 	The highest power ball number can be drawn
*/
private int lottoPowerball(int max) {
    Random random = new Random();
    int powerball;
    List<Integer> list = new ArrayList<Integer>(max);
    
    // Have a list of 1 .. 10
    for (int i = 1; i < (max + 1); i++) {
    	list.add(i);
    }
    
    // Draw a power ball number from the list above
    powerball = list.remove(random.nextInt(max));
    
    return powerball;
}

Generate a complete lotto ticket

Below is a complete lotto ticket generator program. Hope you have fun running it.

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class Lotto {

    public static void main(String[] args) {
	Lotto lotto = new Lotto();
	List<String> dips = new ArrayList<String>();
	String dip = "";
		
	int n = 8; 	// How many lines you want to buy
	int x = 6; 	// how many numbers in a lotto row
	int y = 40; 	// The highest lotto number can be drawn
	int z = 10; 	// The highest power ball number can be drawn


	for (int i = 0; i < n; i++) {
			
	    do {
	        dip = lotto.generateLottoRow(6, 40) + "    " + lotto.drawPowerBall(10);
	    } while (lotto.checkExistence(dip, dips));
			
	    dips.add(dip);
	}
		
	// Print out all dips (a lotto ticket)
	for (int i = 0; i < n; i++) {
	    System.out.println(dips.get(i));
	}
    		
    }
    	
    /*
     * Function description:
     *      Check if the newly generated line (a dip) already exists
     * 
     * Parameters: 
     * 	    dip:    The newly generated lotto line
     *      dips:   The list contains existing lotto lines 
    */
    private boolean checkExistence(String dip, List<String> dips) {
    	int size = dips.size();
    		
    	for (int i = 0; i < size; i++) {
    	    if (dip.equals(dips.get(i))) {
    	        return true;
    	    }
    	}
    		
    	return false;
    }
    	
    /*
     * Function description:
     *     Generate and return a lotto row
     * 
     * Parameters: 
     *     n:       How many numbers in a lotto row
     *     max:     The highest number can be drawn
   */
    private String generateLottoRow(int n, int max) {
        Random random = new Random();
        List<Integer> row = new ArrayList<Integer>(n);
        String rowStr = "";
        List<Integer> list = new ArrayList<Integer>(max);
    
    
        // Have a list of 1 .. max
        for (int i = 1; i < (max + 1); i++) {
    	    list.add(i);
        }
    
        // Create a row of n random numbers from the list above
        for (int i = 0; i < n; i++) {
    	    row.add(list.remove(random.nextInt(max - i)));
        }
    
        // Sort ascending
        Collections.sort(row);
        
        // Generate a lotto line
        for (int i = 0; i < n; i++) {
            rowStr += row.get(i) + " ";
        }
    
        return rowStr;
    }
    	
    /*
     * Function description:
     *      Draws and returns a power ball number
     * 
     * Parameters: 
     *      max:   The highest power ball number can be drawn
    */
    private int drawPowerBall(int max) {
    	Random random = new Random();
        int powerball;
        List<Integer> list = new ArrayList<Integer>(max);
    
    
        // Have a list of 1 .. 10
        for (int i = 1; i < (max + 1); i++) {
            list.add(i);
        }
        
        // Draw a power ball number from the list above
        powerball = list.remove(random.nextInt(max));
        
        return powerball;
    }
}