Results 1 to 6 of 6
  1. #1
    Psyclone is offline Member
    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    0

    Default Non-Repeating Random Integers

    I trying to make a list of 100 random numbers between 1 and 100 without any of them repeating. I built a boolean array and assigned the value true as each number was chosen inside a for-if loop.

    The problem is that sometimes it takes about 600-800 iterations.

    I was wondering if there was a better way to do this. Is there a method to do this already in Java that isn't as intensive?

    I was thinking of making a list of 100 random numbers and then just ranking them and using the ranking as my 100 random numbers since I wouldn't have any repeating. I'm not sure if there is a way to rank a list of 100 numbers though or how intensive it would be.

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

    Default

    Quote Originally Posted by Psyclone View Post
    I trying to make a list of 100 random numbers between 1 and 100 without any of them repeating. I built a boolean array and assigned the value true as each number was chosen inside a for-if loop.

    The problem is that sometimes it takes about 600-800 iterations.

    I was wondering if there was a better way to do this. Is there a method to do this already in Java that isn't as intensive?

    I was thinking of making a list of 100 random numbers and then just ranking them and using the ranking as my 100 random numbers since I wouldn't have any repeating. I'm not sure if there is a way to rank a list of 100 numbers though or how intensive it would be.
    Have a look at the Collections.shuffle( ... ) method. If you want to generate n random numbers from a set much larger than n it works to generate a possible number, check if it is used already, and if so try again. This method gets slower and slower when the size of set set is nearer to n. e.g. the last number from your set only has a probability of 1/100 for not being used already. You don't want to retry and retry until you have randomly found that one unused number.

    kind regards,

    Jos

  3. #3
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    Why cant you just make a list that goes 1-100 and shuffle it using Collections.shuffle?

  4. #4
    Psyclone is offline Member
    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    0

    Default

    Thanks for the help. I actually did end up making a list from 1-100, shuffling it, then adding the value to my array.

    It works great.

  5. #5
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Another way of doing this is to make an ArrayList.

    Java Code:
    import java.util.ArrayList;
    import java.util.Random;
    
    
    public class randomGenerator {
    	
    	private ArrayList<Integer> generate;
    	private final int INITIAL_SIZE = 100;
    	
    	public randomGenerator() {
    		generate = new ArrayList<Integer>(INITIAL_SIZE);
    		fillList();
    	}
    	
            //Each element will store a number 0-99
    	private void fillList() {
    		for (int i=0; i<INITIAL_SIZE; i++)
    			generate.add(i);
    	}
    
            
    	public void printRandomInt() {
    		Random pseudoNumber = new Random();
    		int getThisElement;
    		
    		for (int i=0; i<INITIAL_SIZE; i++) {
    			getThisElement = pseudoNumber.nextInt(generate.size());
    			System.out.print(generate.remove(getThisElement) + " ");
    		}
    
    		
    	}
    }
    Java Code:
    public class randomGenerateDriver {
    	public static void main(String[] args) {
    		randomGenerator test = new randomGenerator();
    		test.printRandomInt();
    
    	}
    }
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  6. #6
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    Yes lil_aziz, that is sort of like the fisher-yates shuffle, but you dont need an array list, just and array, if you were going to use a list, why not just use collections.shuffle()?
    Fisher-Yates shuffle - Wikipedia, the free encyclopedia
    There is a java implementation of this shuffle on that Wiki page.

Similar Threads

  1. Replies: 14
    Last Post: 10-19-2009, 10:57 AM
  2. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  3. Replies: 1
    Last Post: 07-04-2008, 05:39 PM
  4. Random Integers
    By www.kwalski.com in forum Java Applets
    Replies: 8
    Last Post: 12-09-2007, 05:49 PM
  5. adjacent repeating letters
    By artemis_r2 in forum New To Java
    Replies: 1
    Last Post: 11-17-2007, 04:48 AM

Posting Permissions

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