0

Hi how do i avoid getting a duplicate "lottery ticket" in my code?

The problem now is everything works fine, the sorting , the randoming numbers , the making it into a String. The problem is that if i for example want 5 lottery tickets than it i will print out 5 lottery tickets

Example

Lottery tickets: 
--------------
[5, 13, 18, 22, 23, 23, 30]
[4, 7, 10, 12, 16, 26, 32]
[3, 5, 9, 22, 23, 25, 34]
[4, 5, 15, 18, 19, 19, 26]
[3, 3, 12, 14, 23, 26, 35] (Duplicate 3)
[5, 6, 12, 13, 14, 15, 26]

As you can see there is duplicate numbers, i want to avoid this so it will always random. How do i avoid this?

import java.util.Arrays;
import java.util.Scanner;


public class Alt2 {

public static void main(String[] args) {
    System.out.print("How many lottery tickets do u want?: \n");
    Scanner svar= new Scanner(System.in);
    int nr = svar.nextInt();
    System.out.println("Lottery tickets: ");
    System.out.println("--------------");
    for(int i=0; i<nr; i++)  
    {  
        Alt2 t = new Alt2();

        int[] lottoNummer = t.BubbleSort();
        System.out.print(Arrays.toString(lottoNummer));


        System.out.println();
        svar.close();
    }  

}  
private int[] getTicket(){


int[] lottoNummer = new int[7];



lottoNummer[0] = (int) ((35 * Math.random()) + 1);  
lottoNummer[1] = (int) ((35 * Math.random()) + 1);  
lottoNummer[2] = (int) ((35 * Math.random()) + 1);  
lottoNummer[3] = (int) ((35 * Math.random()) + 1);  
lottoNummer[4] = (int) ((35 * Math.random()) + 1);  
lottoNummer[5] = (int) ((35 * Math.random()) + 1);
lottoNummer[6] = (int) ((35 * Math.random()) + 1);



return lottoNummer;  

}

public int[] BubbleSort( )
{
     int j;
     boolean flag = true;   
     int temp;   
     Alt2 t = new Alt2();
     int[] lottoNummer = t.getTicket();

     while ( flag )
     {
            flag= false;    
            for( j=0;  j < lottoNummer.length -1;  j++ )
            {
                   if ( lottoNummer[ j ] > lottoNummer[j+1] )   
                   {
                           temp = lottoNummer[ j ];                
                           lottoNummer[ j ] = lottoNummer[ j+1 ];
                           lottoNummer[ j+1 ] = temp;
                          flag = true;                
                  } 
            } 
      } 
     return lottoNummer;
} 



}
2
  • In your getTicket method, you can implement a loop to keep generating random numbers until there are no duplicates. Commented Feb 6, 2015 at 15:49
  • stackoverflow.com/questions/28095667/… Commented Feb 6, 2015 at 15:50

4 Answers 4

2

you could put sorted numbers 1 to 35 in an arrayList, shuffle them, and then take the first 7 numbers from the list.

//define ArrayList to hold Integer objects
 ArrayList<Integer> numbers = new ArrayList<Integer>();

 for(int i = 0; i < 35; i++)
 {
   numbers.add(i+1);
 }

 Collections.shuffle(numbers);
Sign up to request clarification or add additional context in comments.

Comments

1

I suggest creating an int array of size 0 at start. Then, make a loop to generate your randomized numbers, and another inner loop to run through the array and check if the randomized number already exists. If it is, reduce the main loop index by 1. If it isn't, then create a temporary array of size the main array + 1, make a loop of the main array's size, transfer the values to the temporary array and out of the loop add the last randomized number in the main array's length index.

Just an example:

        int[] nums = new int[0];
        Random rnd = new Random();
        for (int i = 0; i < 30; i++)
        {
            bool dup = false;
            int rand = rnd.Next(1, 99);
            for (int j = 0; j < nums.Length; j++)
            {
                if (nums[j] == rand)
                {
                    i--;
                    dup = true;
                    break;
                }
            }
            if (!dup)
            {
                int[] tmp = new int[nums.Length+1];
                for (int j = 0; j < nums.Length; j++)
                    tmp[j] = nums[j];
                tmp[nums.Length] = rand;
                nums = tmp;
            }
        }

It's untested and I just wrote it. I hope it helps.

Comments

0

How about this:

Make an array list with integers from 1 to 35:

ArrayList<Integer> list = new ArrayList<Integer>();

for (int i = 0; i < 36; i++) {
  list.add(new Integer(i));
}

Then draw a random number from this list, and remove it from the list:

lottoNummerIndex = (int) ((35 * Math.random()) + 1);
lottoNummer[0] = list.get(lottoNummerIndex);
list.remove(lottoNummerIndex);

Draw the next number from the remaining list:

lottoNummerIndex = (int) ((34 * Math.random()) + 1);
lottoNummer[1] = list.get(lottoNummerIndex);
list.remove(lottoNummerIndex);

and repeat this until:

lottoNummerIndex = (int) ((29 * Math.random()) + 1);
lottoNummer[6] = list.get(lottoNummerIndex);
list.remove(lottoNummerIndex);

Now your array lottoNummer contains 7 different numbers.

3 Comments

Note that at every next draw we have one number less, so when you draw the second number, you multiply the random number by 34, and the next by 33.
hmm .. did that and it still gives me duplicates :/ [2, 2, 6, 7, 22, 26, 33]
Sorry, I was mistaken: we should use the number we have drawn as the index of the next random number from the array list, so: lottoNummerIndex = (int) ((35 * Math.random()) + 1); lottoNummer[0] = list.get(lottoNummerIndex); list.remove(lottoNummerIndex);
-1

@Edward Orlowski

So i got an erro

ArrayList<Integer> list = new ArrayList<Integer>();

for (int i = 0; i < 36; i++) {
  list.add(new Integer(i));
}

lottoNummer[0] = (int) ((35 * Math.random()) + 1);  
list.remove(lottoNummer[0]);
lottoNummer[1] = (int) ((35 * Math.random()) + 1); 
list.remove(lottoNummer[1]);
lottoNummer[2] = (int) ((35 * Math.random()) + 1); 
list.remove(lottoNummer[2]);
lottoNummer[3] = (int) ((35 * Math.random()) + 1); 
list.remove(lottoNummer[0]);
lottoNummer[4] = (int) ((35 * Math.random()) + 1);  
list.remove(lottoNummer[4]);
lottoNummer[5] = (int) ((35 * Math.random()) + 1);
list.remove(lottoNummer[5]);
lottoNummer[6] = (int) ((35 * Math.random()) + 1);
list.remove(lottoNummer[6]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.