# Do while loop to generate 6 random numbers

• March 2nd, 2014, 09:04 PM
Bran.NET
Do while loop to generate 6 random numbers
Good evening,
I am fairly new to Java and I am currently taking a class in the field. My assignment is to generate 6 unique random numbers using a "Do While" expression. I might be mistaken but doesnt the inner loop execute first and then it works its way out? With this logic I believe my code should work but then again its not. Please help me if you can. Here is my code:

public class DoLottery
{
public static void main (String args[])
{
int max= 10;
int random;
int random2;
int random3;
int random4;
int random5;
int random6;

do{
random= (int)((Math.random()*max)+1);

do{
random2= (int)((Math.random()*max)+1);

do{
random3= (int)((Math.random()*max)+1);

do{
random4= (int)((Math.random()*max)+1);

do{
random5= (int)((Math.random()*max)+1);
random6= (int)((Math.random()*max)+1);

}while(random6 == random5);

System.out.println(random5);
System.out.println(random6);

}while(random4 == random5 && random4 == random6);

System.out.println(random4);

}while(random3 == random4 && random3 == random5 && random3 == random6);

System.out.println(random3);

}while(random2 == random3 && random2 == random4 && random2 == random5 && random2 == random6);

System.out.println(random2);

}while(random == random2 && random == random3 && random == random4 && random == random5 && random == random6);

System.out.println(random);

//System.out.println(random);
//System.out.println(random2);
//System.out.println(random3);
//System.out.println(random4);
//System.out.println(random5);
//System.out.println(random6);

Again, I am a newbie so be gentle....sidenote: I originally had output at the end but decided to comment it out to see if code would execute if I placed it every time the test was true.

Thanks
• March 2nd, 2014, 11:11 PM
dicdic
Re: Do while loop to generate 6 random numbers
Welcome! Please read Announcements - What's Wrong With My Code? to learn how to post code correctly and other useful info for new members.

I suggest you to read this link: Java Loops - for, while and do...while
and analyze how loop works.
do while loop is a kind of loop that executes atleast once. the last statement in this loop is while(condition) that checks
if its going to do the same process again (which is the statements inside the do while loop)
I'll give an example on how to do it
code below is an example of printing hello world 5 times using do while:
Code java:

```public class HelloWorld {   public static void main(String[] args) { int counter = 1; // this will the counter, we're going to count for 5 do { System.out.println("Hello World"); counter = counter + 1; // adds one to counter } while(counter <= 5); // it checks if the counter is equal or less than 5 // while statement here is a condition, it will do // the printing as long as counter is less than or equal to 5 } }```

i think you have many nested loop in your code.
but i think a single do while loop will do.

if you just need to print random numbers, create a do while loop
and print inside it a random random, but make sure you create
a counter that counts 1 - 6. and your condition must be checking
• March 3rd, 2014, 08:29 AM
Bran.NET
Re: Do while loop to generate 6 random numbers
Thank you, I will use the code tags in the future. I understand that "Do While" statements will always execute the code once before testing and I understand that the "while" statements will test before the code executes. I understand that i can do this all in one "Do While" statement but for some reason my assignment calls for multiple "Do Whiles". I do like the counter idea. That would eliminate all those variables!
• March 3rd, 2014, 10:00 AM
GregBrannon
Re: Do while loop to generate 6 random numbers
And how will you make sure that the program generates 6 unique random numbers?
• March 4th, 2014, 05:30 PM
Bran.NET
Re: Do while loop to generate 6 random numbers
I have no idea...I have mad 4 different variations and still can not do it
• March 4th, 2014, 11:45 PM
Mugambbo
Re: Do while loop to generate 6 random numbers
Hello Bran. How about using an array to store the numbers; then using the nextInt() method from Java.util.Random in a for loop; and finally a simple if statement to ensure the numbers are unique. Wouldn't that simplify everything further and Keep It Simple?