# Thread: random numbers with no duplicate

1. Member
Join Date
Nov 2011
Posts
50
Rep Power
0

## random numbers with no duplicate

hey guys i need random numbers from 0 to 19 without duplicate

i hope you can help me thanks

2. ## Re: random numbers with no duplicate

What you want is called sampling without replacement. You can do it by putting the numbers 0 to 19 in an array, then randomly scrambling the array and reading the numbers one at a time.

3. Member
Join Date
Apr 2011
Posts
73
Rep Power
0

## Re: random numbers with no duplicate

you could just generate a random and place it in an arraylist. THe next one you generate verify if it is contained in the arraylist, if not add it and if it is generate another one until it is not in the arraylist.

4. Member
Join Date
Nov 2011
Posts
50
Rep Power
0

## Re: random numbers with no duplicate

Originally Posted by d3n1s
you could just generate a random and place it in an arraylist. THe next one you generate verify if it is contained in the arraylist, if not add it and if it is generate another one until it is not in the arraylist.
i know but although im doing there are duplicate.

there must be mistake can you write simple basic code for that

5. ## Re: random numbers with no duplicate

There is no need to implement those nitty gritty details yourself; read the API documentation for the Collections.shuffle( ... ) method.

kind regards,

Jos

6. Member
Join Date
Apr 2011
Posts
73
Rep Power
0

## Re: random numbers with no duplicate

Everytime i think of something cleaver to solve a problem JosAH comes in and goes sorry you wasted your time this method does it for you, damn you! :p

7. ## Re: random numbers with no duplicate

Originally Posted by d3n1s
Everytime i think of something cleaver to solve a problem JosAH comes in and goes sorry you wasted your time this method does it for you, damn you! :p
Feel free to implement it all yourself, but make sure you implement it right, i.e. every permutation should have an equal probability. Lazy bones like me, leave those gory details to the core classes and their methods ;-)

kind regards,

Jos

8. Member
Join Date
Nov 2011
Posts
50
Rep Power
0

## Re: random numbers with no duplicate

i researches shuffle find this
Java Code:
```Random rgen1 = new Random();  // Random number generator
int[] cards = new int[20];

//--- Initialize the array to the ints 0-19
for (int i=0; i<cards.length; i++) {
cards[i] = i;
}

//--- Shuffle by exchanging each element randomly
for (int i=0; i<cards.length; i++) {
int randomPosition = rgen1.nextInt(cards.length);
int temp = cards[i];
cards[i] = cards[randomPosition];
cards[randomPosition] = temp;
System.out.println(temp);
btn[i] =new JButton(Integer.toString(i), new ImageIcon(rsm[list[randomPosition]]));
}```
my normal still here is:
Java Code:
```while(k<=19){
boolean duplicate = true;
a:	for(int i = 0; i<list.length; i++) {
Random random = new Random();
num = random.nextInt(19);
if(list[i] == num){
duplicate = true;
break a;
}
else
duplicate=false;

}
if(duplicate==false){

duplicate=true;
list[k]=num;
System.out.println(list[k]);
btn[k] =new JButton(Integer.toString(k), new ImageIcon(rsm[list[k]]));
k++;
}
}```
cant solved :( /cry
Last edited by KSBeyaz; 12-03-2011 at 10:05 PM.

9. Member
Join Date
Nov 2011
Posts
50
Rep Power
0

## Re: random numbers with no duplicate

guys come on plz help

10. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
14

## Re: random numbers with no duplicate

i need random numbers from 0 to 19 without duplicate
If your intention is to obtain a permutation of the first twenty whole numbers just do that. Ie remove the button business which is a separate problem.

Think very seriously about doing the shuffle (in one line?) with Collections.shuffle() as has been suggested. The shuffle() method actually requires that you pass a list rather than an array: see the example on the The List Interface page of Oracle's Tutorial which prints the words in its argument list in a random order.

11. Member
Join Date
Nov 2011
Posts
50
Rep Power
0

## Re: random numbers with no duplicate

Originally Posted by pbrockway2
If your intention is to obtain a permutation of the first twenty whole numbers just do that. Ie remove the button business which is a separate problem.

Think very seriously about doing the shuffle (in one line?) with Collections.shuffle() as has been suggested. The shuffle() method actually requires that you pass a list rather than an array: see the example on the The List Interface page of Oracle's Tutorial which prints the words in its argument list in a random order.
thank you for helping guys

i solved my problem with this code
Java Code:
```ArrayList<Integer> numbers = new ArrayList<Integer>();
for(int i = 0; i < 19; i++)
{
}

Collections.shuffle(numbers);

for (int i = 0; i < numbers.size(); ++i) {
list[i]=numbers.get(i);
btn[i] =new JButton(Integer.toString(i), new ImageIcon(rsm[list[i]]));
System.out.println(list[i]);
// Use arrayList.get(i) and arrayList.get(j).
}
btn[19] =new JButton("19", new ImageIcon(rsm[19]));```

12. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
14

## Re: random numbers with no duplicate

Well done.

Sometimes you see a variant of this like:

Java Code:
```// int list[] = ...
// assign to list[] in order 0,1,2,...,19

Collections.shuffle(Arrays.asList(list));

// now the array is shuffled```

13. Member
Join Date
Nov 2011
Posts
50
Rep Power
0

## Re: random numbers with no duplicate

Hey guys again;

this topic was for my project "PicturePuzzle".

As your help i have completed my project.

now i did upload to it my webside but there is a problem.

i tried to search this error and have learned i should do signed applet.

how can i do ?
thank you

http://ksbeyaz.webuda.com/PicurePuzzle/

14. ## Re: random numbers with no duplicate

Can you put all of your images in the jar file? There won't be any permission required to read the images from the jar file.
You will need to change your code to treat the image files as resources vs disk files. You will use the Class class's getResource methods to get to the image files in your jar file. Read the API doc for the Class class.
Search the forum for sample code that uses the getResource methods.

Another problem you will have with your applet is that it will only work with java version 1.7
User's with version 1.6 will get an error when they try to execute it.

15. Member
Join Date
Nov 2011
Posts
50
Rep Power
0

## Re: random numbers with no duplicate

i have installed 1.7 and deleted 1.6 still same problem occur :(

16. ## Re: random numbers with no duplicate

The problem is that I do NOT have 1.7 and your java applet requires java 1.7 to execute.
I get this error:
java.lang.UnsupportedClassVersionError: PicturePuzzle : Unsupported major.minor version 51.0

17. Member
Join Date
Nov 2011
Posts
50
Rep Power
0

## Re: random numbers with no duplicate

?? i have 1.7 atm but still same problem

18. ## Re: random numbers with no duplicate

If you would compile the class files on your web site with version 1.6 then everyone with version 1.6 AND 1.7 could execute your applet. With the applet class files compiled with version 1.7, ONLY people with version 1.7 can execute your applet.

19. Member
Join Date
Nov 2011
Posts
50
Rep Power
0

## Re: random numbers with no duplicate

Originally Posted by Norm
If you would compile the class files on your web site with version 1.6 then everyone with version 1.6 AND 1.7 could execute your applet. With the applet class files compiled with version 1.7, ONLY people with version 1.7 can execute your applet.
i get it what did you mean.

But i have 1.7 and i get different error.

like this:

20. ## Re: random numbers with no duplicate

See post #14. Put everything in the jar file.

Page 1 of 3 123 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•