# 6 unique random numbers

Page 1 of 1

## 4 Replies - 11231 Views - Last Post: 19 December 2008 - 08:25 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=77498&amp;s=96852ecfeb6eedd000a7714e45c510f3&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Learn2LoveJava

Reputation: 0
• Posts: 10
• Joined: 15-December 08

# 6 unique random numbers

Posted 19 December 2008 - 06:25 AM

```import java.util.Random;

import javax.swing.JOptionPane;

public class randomNODUP {

public static void main(String[] args) {
String output = "";

int[] array1 = new int[6];

// copy random numbers into array1
for (int loop = 0; loop < 6; loop++) {
array1[loop] = rand1to49();
for (int loop2=0;loop2<6;loop++){
if (array1[loop]==array1[loop2]){
loop--;
}
}output = output + array1[loop];
}

JOptionPane.showMessageDialog(null, output + "\n", "Lottery",
JOptionPane.INFORMATION_MESSAGE);

}// end main
public static int rand1to49() {
int num;
// create a random number generator
Random numGenerator = new Random();
// generate a random number between 1 and 49 inclusive
num = Math.abs(numGenerator.nextInt(48)) + 1;
return num;

}// end random
}
```

I want 6 random numbers from 1 to 49, with no duplicates and then print them to the screen and store them to a 2d array(which might have values in already). For some reason it is not working! I know there are more advanced and easier ways to do this so any reply please stay on my beginner level.

Is This A Good Question/Topic? 0

## Replies To: 6 unique random numbers

### #2 janotte

• code > sword

Reputation: 991
• Posts: 5,141
• Joined: 28-September 06

## Re: 6 unique random numbers

Posted 19 December 2008 - 06:34 AM

I'd suggest having a read through these:
http://www.cs.genese...nce/random.html
http://www.javapract...Action.do?Id=62
http://www.glenmccl.com/tip_010.htm
http://java.sun.com/...til/Random.html
and then starting your code again with a clearer idea of what you are trying to design.

Not trying to be rude but it's a bit of a mess at the moment and you might be better starting from scratch.

This post has been edited by janotte: 19 December 2008 - 06:39 AM

### #3 AmitTheInfinity

• C Surfing ∞

Reputation: 119
• Posts: 1,565
• Joined: 25-January 07

## Re: 6 unique random numbers

Posted 19 December 2008 - 06:36 AM

```
for (int loop = 0; loop < 6; loop++)
{
int loop2; // you need this to be declared outside for loop as you need to use it outside following for loops scope as well.
array1[loop] = rand1to49();
for (loop2=loop-1;loop2>=0;loop--) // you are supposed to check with numbers generated, which are stored before
//																	  current number and not next locations! so you are supposed to check backwards.
{
if (array1[loop]==array1[loop2])
{
loop--;
break;
}
}
if(loop2<0) // This means the generated number is unique, else for loop would have broken in between before reaching -1.
output = output + array1[loop];
}

```

### #4 Learn2LoveJava

Reputation: 0
• Posts: 10
• Joined: 15-December 08

## Re: 6 unique random numbers

Posted 19 December 2008 - 06:46 AM

AmitTheInfinity, on 19 Dec, 2008 - 05:36 AM, said:

```
for (int loop = 0; loop < 6; loop++)
{
int loop2; // you need this to be declared outside for loop as you need to use it outside following for loops scope as well.
array1[loop] = rand1to49();
for (loop2=loop-1;loop2>=0;loop--) // you are supposed to check with numbers generated, which are stored before
//																	  current number and not next locations! so you are supposed to check backwards.
{
if (array1[loop]==array1[loop2])
{
loop--;
break;
}
}
if(loop2<0) // This means the generated number is unique, else for loop would have broken in between before reaching -1.
output = output + array1[loop];
}

```

Thank you very very much, I kind of see what is going on here but the only thing is it doesnt print anything out, the windows doesnt even pop up ;(

### #5 baavgai

• Dreaming Coder

Reputation: 6979
• Posts: 14,601
• Joined: 16-October 07

## Re: 6 unique random numbers

Posted 19 December 2008 - 08:25 AM

The logic is not bad, actually. It's the implementation that's messing you up.

Your big error is the inner loop declaration, it has all kinds of issues. There are two more errors.

```Random numGenerator = new Random();
String output = "";

int[] array1 = new int[6];

// copy random numbers into array1
for (int loop = 0; loop < 6; loop++) {
array1[loop] = Math.abs(numGenerator.nextInt(48)) + 1;
// this is wrong in two ways
// it increments loop instead of loop2 and it goes to the end
//for (int loop2=0;loop2<6;loop++){
// note, we don't want to check the last value
// that's the test value
for (int loop2=0;loop2<loop; loop2++){
if (array1[loop]==array1[loop2]){
loop--; // this is a little wonky, but can work
// make sure you leave this loop immediately
// you've messed with the value in loop
break;
}
}
// oops, output doesn't know if we did a loop--
// we'll make output later
// output = output + array1[loop];
}

for (int loop = 0; loop < 6; loop++) {
// spaces, numbers make more sense with some in between
output = output + array1[loop] + " ";

}

JOptionPane.showMessageDialog(null, output + "\n", "Lottery",
JOptionPane.INFORMATION_MESSAGE);

```

Personally, I'd make a couple of methods to clear this up in main. The random number generator was not one of them, though, since it's essentially a one liner.