# array of non repeating random numbers

Page 1 of 1

## 5 Replies - 7556 Views - Last Post: 19 July 2011 - 05:18 AMRate Topic: 1 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=240062&amp;s=bb805e65fd288982bc8f9519bbeaa31b&md5check=' + ipb.vars['secure_hash'], cur_rating: 5, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 masterjohji

Reputation: 1
• Posts: 23
• Joined: 19-June 11

# array of non repeating random numbers

Posted 18 July 2011 - 11:33 PM

this is what I've done so far ... can you tell me where I've gone wrong ,this code is supposed to give me non repeating random numbers from 1~6 , but when I try to run it gives me results like 1-5-5-4-3-2 , 2-3-4-4-5-6...it looks like I got the first two numbers correct because they are always different each time I run it but I'm not very sure tho.

```                                  for(int x=0;x<=5;x++)
{	score[x]=(int)(1+Math.random()*6);
if(x>=1)
{
int x2 = x-1;
for(int c=0;c<=x2;c++)
{
if(score[c]==score[x])
score[x]=(int)(1+Math.random()*6);
}
}
}
```

This post has been edited by masterjohji: 18 July 2011 - 11:35 PM

Is This A Good Question/Topic? 0

## Replies To: array of non repeating random numbers

### #2 fromTheSprawl

• Monomania

Reputation: 515
• Posts: 2,063
• Joined: 28-December 10

## Re: array of non repeating random numbers

Posted 18 July 2011 - 11:47 PM

It's because you don't have any conditions checking if the random number already produced is already provided once. What you should do is create another array or preferably an arraylist, then when you encounter a random number, check if it exists on the list of the numbers already existing, if not, add it to the list, if it exists, loop again. I believe you should use a do while loop with this one.

### #3 smohd

• Critical Section

Reputation: 1820
• Posts: 4,627
• Joined: 14-March 10

## Re: array of non repeating random numbers

Posted 18 July 2011 - 11:51 PM

You have a problem with a loop, if the next time the number generated is the same you dont check for it again, instead you go forward. In your inner for loop, if the number generated matches, just restart the loop to start again. Something like:
```if(score[c]==score[x]){
score[x]=(int)(1+Math.random()*6);
c =-1;}
```

Also you may look about shuffling in java collection:
http://leepoint.net/...-shuffling.html
http://www.roseindia...rateRandom.html
http://javafaq.nu/java-article719.html

### #4 masterjohji

Reputation: 1
• Posts: 23
• Joined: 19-June 11

## Re: array of non repeating random numbers

Posted 19 July 2011 - 01:05 AM

OK thanks guys , I already fixed my mistakes

here's my code now.

```                          for(int x=0;x<=5;x++)
{
score[x]=(int)(1+Math.random()*6);
if(x>0)
for(int c=0;c<=x-1;c++)
if(score[c]==score[x])
x--;
}
```

### #5 smohd

• Critical Section

Reputation: 1820
• Posts: 4,627
• Joined: 14-March 10

## Re: array of non repeating random numbers

Posted 19 July 2011 - 01:17 AM

### #6 g00se

• D.I.C Lover

Reputation: 3110
• Posts: 13,431
• Joined: 20-September 08

## Re: array of non repeating random numbers

Posted 19 July 2011 - 05:18 AM

Quote

this code is supposed to give me non repeating random numbers from 1~6

It would be easier to do:

```	Integer[] a = { 1, 2, 3, 4, 5 };
List<Integer> nums = new ArrayList<Integer>(Arrays.asList(a));
for(Integer i : a) {
Integer score = nums.remove((int)(Math.random() * nums.size()));
System.out.println(score);
}

```