Oh no sorry it can have the same number in each different line. For example two of the lines can read:

1 4 5 2 6 8 9 Powerball 9
2 5 6 3 7 9 10 Powerball 8

It cannot read:

1 4 4 5 8 9 10 Powerball 8 (This isn't a good line because two 4's were generated in the line. The seven in each line has to be unique. The other lines can have numbers that were in other lines. Just duplicate numbers can't be in the same line.

in that case gimme 30 mins to try something. till I do, you should try making a while loop to create a new number as long as the number is already there in the array :

while (number is there in loop (check using another for loop :( ))
{
create new number
}

Think of a way to keep track if a number has been used already. If it has ask the random number generator to give you a new number. Continue testing and asking until you get a new number. Then record that number so that it's not given again.

I think this should do it :)

import java.util.Random;

public class test //Declare the class
{
	public static void main(String[] args)
	{
		int number;
		int pick; // Declare pick as integer
		 //Declare an array to hold 7 random numbers.
		 //This array will be re-written on after each line is completely printed.
		int draw[] = new int[7];
		//let all the elements in the array be 0
		for (int i = 0; i <7; i++)
		{
			draw[i]=0;
		}
		int pwrBall = 10; // The range for the Power Ball
		int BallNum=0;
		
		// Randomly pick seven numbers from 1 to 35

		for (int i = 0; i <=8; i ++)
		{
			//Create a power ball Random Number
			BallNum = (int) (pwrBall * Math.random()) + 1;
			
			for (pick = 0; pick < 7; pick ++)
			{	//pick a random number
				number = (int) (35 * Math.random()) + 1;
				
				//check if the number is already been chosen
				for (int j = 0; j < 7; j++)
				{
					while (number == draw[j])
					{
						//pick a new number if it is already there
						number = (int) (35 * Math.random()) + 1;
					}
				}		
					
				draw[pick] = number;//Add the number to the array
				
				// Print output of each ball
				System.out.printf(draw[pick]+"-");
			}		
			System.out.printf(BallNum + "\n");//Print Powerball
			
		}
	}
}

Enjoy :)

I think this should do it

Have you tested it? To more easily test it, change the max number from 35 to 12 and run it a few times.

Have you tested it? To more easily test it, change the max number from 35 to 12 and run it a few times.

i did test it. Although on your advice I think I'll do it again with a smaller margin. Thanks.

EDIT: And it turns out it does not do it O_O darn. i think i know what I'm doing wrong... Lemme try doing some changes to the for loop values...

EDIT 2: AAAAND I GOT IT :D int he while loop just change the value f j to 0.
it becomes :

for (int j = 0; j < 7; j++)
	{
		while (number == draw[j])
		{
			//pick a new number if it is already there
			number = (int) (35* Math.random()) + 1;
			j = 0;				}
		}
        }

So where it says:

while (number == draw[j]

That basically is saying if the number equals a number that was already drawn then it selects a new number?

if the number equals a number

But it only tests against ONE number not all of them.
You need to be a bit more clever. Use the generated number as an index to an array that holds the history.

I'm extremely, extremely new to java so I apologize for showing my obviously naivety with it. I'm trying to learn.

But it only tests against ONE number not all of them.

Since it's in a for loop it DOES test for all of them. Unless Im mistaken O_O.

Its giving me 9 sets instead of 8.

in the outer loop, take out the = in the i <= 8

Thank you so much!! I'm marking this as solved. Thank you all for helping me and teaching me more about Java.

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.