Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Sybase Database Administrators Handbook

Authors: Brian Hitchcock

1st Edition

0133574776, 978-0133574777

More Books

Students also viewed these Databases questions