简体   繁体   中英

Java Simple Lottery Program

I tried to create a simple lottery program. Here is a problem: it still prints same numbers. For example I got 33 21 8 29 21 10 as output. Everytime when random number is generated, code checks if that number is already generated, then it creates a new random number but after that it doesn't check again. I couldn't find a way to do that.

public static void main(String[] args)
{

    int[] lottery = new int[6];
    int randomNum;

    for (int i = 0; i < 6; i++)
    {
        randomNum = (int) (Math.random() * 50); //Random number created here.
        for (int x = 0; x < i; x++)
        {
            if (lottery[i] == randomNum) // Here, code checks if same random number generated before.
            {
                randomNum = (int) (Math.random() * 50);//If random number is same, another number generated.
            }

        }
        lottery[i] = randomNum;
    }

    for (int i = 0; i < lottery.length; i++)
        System.out.print(lottery[i] + " ");

}

You are changing the random number while you are checking it. You need to pick one random number and check whether it is present or not.

BTW A shorter approach is to use a shuffle.

// give me all the number 1 to 50
List<Integer> list = IntStream.range(1, 51).boxed().collect(Collectors.toList());
// shuffle them.
Collections.shuffle(list);
// give me the first 6
System.out.println(list.subList(0, 6));

There are 2 problems with your code:

  • you check if lottery[i] and randomNum are the same, it should be lottery[x]
  • when you re-generate a random number, you don't check it against the first numbers in lottery .

Here is a corrected version:

public static void main(String[] args) {

    int[] lottery = new int[6];
    int randomNum;

    for (int i = 0; i < 6; i++) {
        randomNum = (int) (Math.random() * 50); // Random number created here.
        for (int x = 0; x < i; x++) {
            if (lottery[x] == randomNum) // Here, code checks if same random number generated before.
            {
                randomNum = (int) (Math.random() * 50);// If random number is same, another number generated.
                x = -1; // restart the loop
            }

        }
        lottery[i] = randomNum;
    }

    for (int i = 0; i < lottery.length; i++)
        System.out.print(lottery[i] + " ");

}

A simple solution, between the first (who could be very abstract for a not Java programmer) and the 2nd (not assuring the unicity of the number list).

    Collection<Integer> liste = new ArrayList<Integer>();
    for (int i = 0; i < 6; i++)
    {

        Boolean ap = false; 
        while (!ap)
        {
            Integer randomNumber  =  (int) (Math.random() * 50);

            if (! liste.contains(randomNumber)){

                liste.add(randomNumber);
                ap = true;
            }
        }
    }


    for (Integer liste1 : liste) {
        System.out.print(liste1+" ");

    }

try this one, it creates 12 x (6 out of 45)

public static void main(String[] args) {
    SecureRandom random = new SecureRandom();
    for (int i = 0; i < 12; i++){
        Integer[] tipp = new Integer[6];
        int n = 0;
        do {
            int r = random.nextInt(45) + 1;
            if (Arrays.asList(tipp).indexOf(r)<0){
                tipp[n]= r;
                n++;
            }
        } while (n<=5);
        Arrays.sort(tipp);
        System.out.println(Arrays.toString(tipp));
        }
}
public static void main(String[] arg) {
    int[] lottery = new int[6];
    int randomNum;
    c1:
        for (int i = 0; i < 6; i++) {
            randomNum = (int) (Math.random() * 50); // Random number created here.
            if(randomNum == 0) {
                continue c1;
            }   
            for (int x = 0; x < i; x++) {
                if (lottery[x] == randomNum ) // Here, code checks if same random number generated before.
                {
                    randomNum = (int) (Math.random() * 50);// If random number is same, another number generated.
                    x = -1; // restart the loop
                }

            }
            lottery[i] = randomNum;
        }

    for (int i = 0; i < lottery.length; i++)
        System.out.print(lottery[i] + " ");

}

This is the object class for making a ticket, it will create ONE ticket with ascending values at which whatever parameters you choose. This program won't run until you have a main method that you call. Make sure to import TreeSet.

import java.util.TreeSet;
public class TicketMaker{
private int numbersPerTicket;
private int lowestNumber;
private int highestNumber;

TicketMaker(){
    numbersPerTicket=0;
    lowestNumber=0;
    highestNumber=0;
}
TicketMaker(int numbersPerTicket,int lowestNumber,int highestNumber){
    if(numbersPerTicket > 0 && lowestNumber >= 0 && highestNumber >= lowestNumber){
        this.numbersPerTicket=numbersPerTicket;
        this.lowestNumber=lowestNumber;
        this.highestNumber=highestNumber;
    }
}
public boolean printTicket(int numbersPerTicket,int lowestNumber,int highestNumber){
    if(numbersPerTicket > 0 && lowestNumber >= 0 && highestNumber >= lowestNumber){
        if(numbersPerTicket > highestNumber){
            System.out.println("Error not in-bounds");
            return false;
        } 
        int rand;
        int count=0;
        System.out.println("[Ticket Printed]");
        TreeSet<Integer> set = new TreeSet<>();
        do{
            rand = (int)(Math.random()*highestNumber)+lowestNumber;
            set.add(rand);
            count++;
        }while(set.size() != numbersPerTicket);
        System.out.println(set);
        return true;
    }
    else{
        System.out.println("Error not in-bounds");
        return false;
    }
}
public boolean isValidTicketData(int numbers,int lowest,int highest){
    if(lowest != 1){
        if(highest == numbers)
            return false;
    }
    if(numbers <= highest){
        if(numbers > 0 && lowest >= 0 && highest >= lowest)
            return true;
    }
    return false;
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM