Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By Fubarable

Thread: stop repeat value from random

  1. #1
    nonybrighto is offline Member
    Join Date
    Oct 2012
    Location
    ph
    Posts
    43
    Rep Power
    0

    Default stop repeat value from random

    please i want to make a program were i will generate random numbers. If a number is present, it should not be repeated...please help me.is it possible to do this?
    joy wouldnt feel so gud if it wasnt for pain

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,987
    Rep Power
    9

    Default Re: stop repeat value from random

    Sure, it's possible. Prepopulate a data structure with numbers, then shuffle it, and pull them out one by one. Or fill up a data structure as you generate random numbers, checking the newly generated numbers against what you've already seen.

    What you do really depends on your context and the trade-offs you're willing to make. But chances are this is homework, so figuring out how to do it is the whole point of the assignment.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    nonybrighto is offline Member
    Join Date
    Oct 2012
    Location
    ph
    Posts
    43
    Rep Power
    0

    Default Re: stop repeat value from random

    ...i want to use arraylist to keep the datas and test if its already present.thanks for your help.i'll try it today and come back in case i have any problem. Please its not an assignment.i learn java on my own i want to make a game and i thought of sometin that needs that...:-)
    joy wouldnt feel so gud if it wasnt for pain

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: stop repeat value from random

    But it is homework regardless if it has been assigned by someone else or assigned by you to yourself. Half the fun is trying to figure it out, and we wouldn't want to ruin that. :)
    nonybrighto likes this.

  5. #5
    nonybrighto is offline Member
    Join Date
    Oct 2012
    Location
    ph
    Posts
    43
    Rep Power
    0

    Default Re: stop repeat value from random

    Thanks bro i did it.i used arraylist.if you know any other shorter method u can drop it so i can have multiple knowledge about it....;-)

    Java Code:
    import java.util.ArrayList;
    import java.util.Random;
    
    
    public class oncerandom {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		
    		ArrayList<Integer>  list =new ArrayList<Integer>();
    		
    		Random rand= new Random();
    		
    		
    		for (int i=0;i<=50000;i++)   //so that all numbers must be guessed...
    		{
    		     int number=rand.nextInt(50);
    		     
    		    if(!list.contains(number))
    		    {
    		    	list.add(number);
    		    }
    		}
          for(int i=0;i<list.size();i++)
          {
        	  System.out.printf("%d ",list.get(i));
          }
    	}
    
    }
    Last edited by nonybrighto; 02-16-2013 at 08:13 AM.
    joy wouldnt feel so gud if it wasnt for pain

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: stop repeat value from random

    The nextInt(50) method call produces a number in the range 0 ... 49; it could produces the sequence 0, 0, 0, 0 ... (5000 times); it is not very likely, but it could happen en then you'll end up with a list with one single element (0); I don't think that is what you want. There is an easy way out though: have a look at the Collections.shuffle( ... ) method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    nonybrighto is offline Member
    Join Date
    Oct 2012
    Location
    ph
    Posts
    43
    Rep Power
    0

    Default Re: stop repeat value from random

    what will i shuffle?....the random or the arraylist?....i havn't used it before...
    joy wouldnt feel so gud if it wasnt for pain

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,987
    Rep Power
    9

    Default Re: stop repeat value from random

    Quote Originally Posted by nonybrighto View Post
    what will i shuffle?....the random or the arraylist?....i havn't used it before...
    That's why you'd take a look at the API and tutorials to read about it before using it.

    Once you learn what it does, take a step back and think about how it could be used to solve the problem.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    nonybrighto is offline Member
    Join Date
    Oct 2012
    Location
    ph
    Posts
    43
    Rep Power
    0

    Default Re: stop repeat value from random

    thanks a lot to you all...i will do that now. God bless this site and its users
    joy wouldnt feel so gud if it wasnt for pain

Similar Threads

  1. Random does not repeat itself?
    By swedishfished in forum New To Java
    Replies: 5
    Last Post: 10-20-2012, 05:56 PM
  2. Replies: 18
    Last Post: 04-03-2012, 09:49 AM
  3. New to java stop need help stop
    By Skrap09 in forum New To Java
    Replies: 3
    Last Post: 09-17-2011, 12:45 PM
  4. Use stop button to stop moving (stop timers) on JPanel
    By mneskovic in forum New To Java
    Replies: 3
    Last Post: 07-23-2010, 12:50 PM
  5. Help with a repeat
    By leeavital in forum New To Java
    Replies: 1
    Last Post: 01-01-2010, 09:42 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
  •