# random numbers with no duplicate

Show 40 post(s) from this thread on one page
Page 1 of 3 123 Last
• 12-03-2011, 08:34 PM
KSBeyaz
random numbers with no duplicate
hey guys i need random numbers from 0 to 19 without duplicate

i have already searched entire google but not find any helpfull code

i hope you can help me thanks
• 12-03-2011, 08:46 PM
kjkrum
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.
• 12-03-2011, 08:47 PM
d3n1s
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.
• 12-03-2011, 09:01 PM
KSBeyaz
Re: random numbers with no duplicate
Quote:

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
• 12-03-2011, 09:22 PM
JosAH
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
• 12-03-2011, 09:30 PM
d3n1s
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
• 12-03-2011, 09:34 PM
JosAH
Re: random numbers with no duplicate
Quote:

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
• 12-03-2011, 10:02 PM
KSBeyaz
Re: random numbers with no duplicate
i researches shuffle find this
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:
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
• 12-04-2011, 12:44 AM
KSBeyaz
Re: random numbers with no duplicate
guys come on plz help
• 12-04-2011, 12:55 AM
pbrockway2
Re: random numbers with no duplicate
Quote:

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.
• 12-04-2011, 02:31 AM
KSBeyaz
Re: random numbers with no duplicate
Quote:

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
Code:

```ArrayList<Integer> numbers = new ArrayList<Integer>();     for(int i = 0; i < 19; i++)     {       numbers.add(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-04-2011, 03:05 AM
pbrockway2
Re: random numbers with no duplicate
Well done.

Sometimes you see a variant of this like:

Code:

```// int list[] = ... // assign to list[] in order 0,1,2,...,19 Collections.shuffle(Arrays.asList(list)); // now the array is shuffled```
• 12-06-2011, 10:00 PM
KSBeyaz
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.

when it is loading it gives error like this

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

how can i do ?
thank you

here is my webside link
http://ksbeyaz.webuda.com/PicurePuzzle/
• 12-06-2011, 10:54 PM
Norm
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.
• 12-06-2011, 11:40 PM
KSBeyaz
Re: random numbers with no duplicate
i have installed 1.7 and deleted 1.6 still same problem occur :(
• 12-06-2011, 11:42 PM
Norm
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
• 12-06-2011, 11:48 PM
KSBeyaz
Re: random numbers with no duplicate
?? i have 1.7 atm but still same problem
• 12-06-2011, 11:54 PM
Norm
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.
• 12-07-2011, 05:48 PM
KSBeyaz
Re: random numbers with no duplicate
Quote:

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: