Announcement

Collapse
No announcement yet.

Java help - URGENT

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Java help - URGENT

    Hey guys I need to write a Java program that fills an array with random numbers between 1-1000 without any repetition. This code has to be fairly efficient ( I can think of some really inefficient ways to do this ) If you post a solution to this please explain how it works.

    Thanks in advance guys, I really need someone to come through on this one.
    Master of all arts, he can manipulate anything at will Distorting and warping you.
    Brushing all who oppose him of the face of the earth, he follows no pattern and cannot be measured.
    No software can rasterize him - no man can illustrate him - for he is.
    The Exalted

  • #2
    Many different ways!
    Simply generate an array of numbers 1 - 1000
    then use the built in shuffle function: Collections (Java 2 Platform SE v1.4.2)
    then just select say 3 random items from the array. Make a nice little function!
    (Stolen from here: Creating random numbers in an array in Java with no repeats? - Yahoo! Answers (has other methods too))
    Last edited by CerealGOAT; March 29, 2011, 06:15 PM.

    Comment


    • #3
      How is this? does it look good to you guys?

      Code:
      public static void main(String[] args) {
      		
      		Random r = new Random();
      		int[]nums = new int[1000];
      		int[]randnums = new int[50];
      		
      		int i,j,m,idx,temp;
      		
      		//creates array 1000 numbers
      		for(i=0;i<1000;i++) 
      			nums[i]=i;
      		
      		//shuffles array "nums"
      		for(j=0;j<1000;j++){
      			idx = r.nextInt(1001)+1;
      			temp = nums[idx];
      			nums[idx]=nums[j];
      			nums[j]=temp;
      		}
      		
      		//takes required amount of randoms
      		for(m=0;m<50;m++){
      			randnums[m]=nums[m];
      		}
      		
      		
      
      	}
      Master of all arts, he can manipulate anything at will Distorting and warping you.
      Brushing all who oppose him of the face of the earth, he follows no pattern and cannot be measured.
      No software can rasterize him - no man can illustrate him - for he is.
      The Exalted

      Comment


      • #4
        I don't exactly know how your code is supposed to work, but I don't see why there wouldn't be the possibility of the same generated number being added twice.
        Do you have to use a simple array or can you use Java Collections (ArrayList, Vector etc)?
        When just using a simple array you have to search the whole array everytime to see if the number is already in it. This is of course not very efficient.
        When you can use Java Collections you should take a look at the HashSet, which allows every element only once and provides efficient access, so that the check if a number already exists, is efficient.
        When you need some sample code, let me know.

        There is a function which generates a normal array from a Java Collection class (.toArray()). You can use it, if you need to have a normal array as output, but want to have some of the advantages the Collections give you.

        Comment


        • #5
          Problem was solved on IRC earlier. Thread closed. ;)
          sigpic

          WAT WAT WAT WAT WAT WAT WAT WAT WAT WAT WAT WAT WAT WAT


          Comment

          Working...
          X