This code randomly generates 4 lottery tickets and prints them on seperate lines. The code works, but what if I wanted this to print for 100 lottery tickets? Is there a way to write this with less lines of codes? Would using a for loop work?

import java.util.Collections;
 import java.util.ArrayList;
 public class Lottery5 {
 
   public static void main(String[] args) {
 
     //define ArrayList to hold Integer objects
     ArrayList<Integer> numbers = new ArrayList<Integer>();
     for(int i = 0; i <= 9; i++)
     {
     numbers.add(i+0);
   }
 
     Collections.shuffle(numbers);
     System.out.print("Lotto Ticket #1:  ");
     for(int j =0; j < 10; j++)
     {
       System.out.print(numbers.get(j) + " ");
     }
	Collections.shuffle(numbers);
     System.out.print("\nLotto Ticket #2:  ");
     for(int j =0; j < 10; j++)
     {
       System.out.print(numbers.get(j) + " ");
     }
     Collections.shuffle(numbers);
     System.out.print("\nLotto Ticket #3:  ");
     for(int j =0; j < 10; j++)
     {
       System.out.print(numbers.get(j) + " ");
     }
	Collections.shuffle(numbers);
     System.out.print("\nLotto Ticket #4:  ");
     for(int j =0; j < 10; j++)
     {
       System.out.print(numbers.get(j) + " ");
     }



   }

}

Recommended Answers

All 6 Replies

Yes. What you want to do is to collide your code from line 14 to 37 into another loop. Just a sample code portion... You may want to modify to whatever way you want...

for (int i=0; i<100; i++) {   // the loop to control how many lottery tickets you want
  Collections.shuffle(numbers);
  System.out.print("\nLotto Ticket #"+(i+1)+":  ");
  for (int j=0; j<10; j++) {  // the loop to control the print out each lottery number in a ticket
    System.out.print(numbers.get(j) + " ");
  }
}

not to be too helpful (not really possible) but here is the final code becuase its kinda hard to figure out where to put his code in the program

import java.util.Collections;
import java.util.ArrayList;
public class Lottery {
	public static void main(String[] args) {
		ArrayList<Integer> numbers = new ArrayList<Integer>();
		for(int i = 0; i <= 9; i++){
			numbers.add(i+0);
		}
		for (int i=0; i<100; i++) {
			Collections.shuffle(numbers);
			System.out.print("\nLotto Ticket #"+(i+1)+":  ");
			for (int j=0; j<10; j++) {
				System.out.print(numbers.get(j) + " ");
			}
		}
	}
}

By the way this inspired me to make a matrix like program.

Member Avatar for ztini

Programmer, I like your method...except for its O(n^2) efficiency (for loop + shuffle) to create 1 ticket. Here's another method that is O(n) efficiency, without all that silly Collection shuffling and ArrayLists.

import java.util.Random;


public class LotteryTicket {
	
	public static String create() {
		String ticket = "";
		for (int i = 0; i < 10; i++)
			ticket += new Random().nextInt(9);
		return ticket;
	}
	
	public static void main(String[] args) {
		for (int i = 1; i <= 4; i++)
			System.out.println("Ticket #" + i + ": " + LotteryTicket.create());
	}
	
}

Console:

Ticket #1: 7285781638
Ticket #2: 4466286832
Ticket #3: 3140770627
Ticket #4: 5661222784

yes, that does seem better but i was just piecing together the already provided code.

Wow, this is my first post and I have to say, wow! Thanks for the quick responses and very helpful input. I will compile and run my program when I get home from work tonight and see how it looks. Again, thank you, and I will let you know how it turns out.

It worked very well, and only 12 lines of code. Thanks guys

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.