Page 1 of 3 123 LastLast
Results 1 to 20 of 47
Like Tree2Likes

Thread: random numbers with no duplicate

  1. #1
    KSBeyaz is offline Member
    Join Date
    Nov 2011
    Posts
    50
    Rep Power
    0

    Default 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

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default 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.
    Get in the habit of using standard Java naming conventions!

  3. #3
    d3n1s is offline Member
    Join Date
    Apr 2011
    Posts
    69
    Rep Power
    0

    Default 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. #4
    KSBeyaz is offline Member
    Join Date
    Nov 2011
    Posts
    50
    Rep Power
    0

    Default Re: random numbers with no duplicate

    Quote Originally Posted by d3n1s View Post
    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. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    d3n1s is offline Member
    Join Date
    Apr 2011
    Posts
    69
    Rep Power
    0

    Default 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. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default Re: random numbers with no duplicate

    Quote Originally Posted by d3n1s View Post
    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
    DarrylBurke and d3n1s like this.
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    KSBeyaz is offline Member
    Join Date
    Nov 2011
    Posts
    50
    Rep Power
    0

    Default 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 09:05 PM.

  9. #9
    KSBeyaz is offline Member
    Join Date
    Nov 2011
    Posts
    50
    Rep Power
    0

    Default Re: random numbers with no duplicate

    guys come on plz help

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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. #11
    KSBeyaz is offline Member
    Join Date
    Nov 2011
    Posts
    50
    Rep Power
    0

    Default Re: random numbers with no duplicate

    Quote Originally Posted by pbrockway2 View Post
    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++)
         {
           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. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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. #13
    KSBeyaz is offline Member
    Join Date
    Nov 2011
    Posts
    50
    Rep Power
    0

    Default 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

    acces denied("java.io.FilePermission" "resim1.4.gif""read")

    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/

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default 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. #15
    KSBeyaz is offline Member
    Join Date
    Nov 2011
    Posts
    50
    Rep Power
    0

    Default Re: random numbers with no duplicate

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

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default 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. #17
    KSBeyaz is offline Member
    Join Date
    Nov 2011
    Posts
    50
    Rep Power
    0

    Default Re: random numbers with no duplicate

    ?? i have 1.7 atm but still same problem

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default 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. #19
    KSBeyaz is offline Member
    Join Date
    Nov 2011
    Posts
    50
    Rep Power
    0

    Default Re: random numbers with no duplicate

    Quote Originally Posted by Norm View Post
    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:

    acces denied("java.io.FilePermission""resim1.5.gif""read ")

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: random numbers with no duplicate

    acces denied("java.io.FilePermission""resim1.5.gif""read ")
    See post #14. Put everything in the jar file.

Page 1 of 3 123 LastLast

Similar Threads

  1. removing duplicate numbers from an array
    By ozzyman in forum New To Java
    Replies: 1
    Last Post: 03-14-2011, 08:22 PM
  2. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  3. Help with random numbers
    By checkmylongboarding in forum New To Java
    Replies: 2
    Last Post: 01-12-2009, 05:47 AM
  4. random string are duplicate
    By googgoo in forum New To Java
    Replies: 3
    Last Post: 04-03-2008, 10:01 AM
  5. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 PM

Posting Permissions

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