Question: Need help with this lattery code I tried to create a simple lottery program. Here is a problem: it still prints same numbers. For example,

Need help with this lattery code

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. Every time 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. Here is my code down below so far

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] + " ");

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!