2

Im trying to generate an array with 1000 integers of non-repeating numbers in ascending order from 0 to 10,000

So far what I have is:

 public static void InitArray(int[] arr) { // InitArray method
     int i, a_num; // int declared
     Random my_rand_obj = new Random(); // random numbers

     for (i = 0; i <= arr.length-1; i++) // for loop
     {
        a_num = my_rand_obj.nextInt(10000); // acquiring random numbers from 0 - 10000
            arr[i] = a_num; // numbers being put into array (previoulsy declared of size 1000)
     }

  }
public static void ShowArray(int[] arr) { // ShowArray method
     int i; // int declared
     for (i = 0; i <= arr.length-1; i++) { // for loop
        System.out.print(arr[i] + " "); // show current array content
     }
     System.out.println(); // empty line
  }
public static void Sort(int[] arr) { // SortArray method
     int i, min, j; // int decalred
     for (i = 0; i < arr.length-1; i++) { // for loop
        min = i; // min is i
        for (j = i + 1; j < arr.length; j++) { // nested for loop
           if (arr[j] < arr[min]) { // if statement
              min = j; // j is the new minimum
           }
        }       
        int swap = arr[min]; // swap "method"
        arr[min] = arr[i];
        arr[i] = swap;
     }  
  }

Is there any way to check the numbers are not repeating? Is there a function besides the random generator that will let me generate numbers without repeating? Thanks for any help

3
  • You can generate numbers simply by iterating over a for loop using the index - it would meet your requirements as stated. Commented Mar 31, 2014 at 7:04
  • 1
    If you want to be sure that there are non-repeating elements, convert your array to a HashSet, sort it and check its size vs the array's size.. (there are several other "efficient" methods as well.). BTW you are re-inventing the wheel .. Set allows only unique values, why not use it instead of array? Commented Mar 31, 2014 at 7:05
  • 1
    How to generate random unique numbers? - This should be a decent read and probably you can then convert the Set/List to an array and sort it using Arrays.sort(). Commented Mar 31, 2014 at 7:08

3 Answers 3

1

You can declare array of size 10,000 and init the array in away that each cell in the array will holds the value of it's index:

int [] arr= new  int[10000];
for (int i=0 i < arr.length; i++){
     arr[i] = i
}

Now you can shuffle the array using java Collections. and take the first 1000 items from the array and sort then using java sort.

Sign up to request clarification or add additional context in comments.

2 Comments

OP needs random nos. from 0-10,000 alright, but OP needs an array of size only 1,000.
That is way he should take the first 1000 items as I mentions in the answer.
1

This will do I believe..

HashSet hs = new HashSet();
for(int i=0;i< arr.length;i++)
    hs.add(arr[i]);
List<Integer> integers=new ArrayList<>(hs);
Collections.sort(integers);

2 Comments

Not fair... I was just gonna write that! ;-)
@ambigram_maker LOL ;-)
0

A very simple solution is to generate the numbers cleverly. I have a solution. Though it may not have an even distribution, it's as simple as it can get. So, here goes:

public static int[] randomSortedArray (int minLimit, int maxLimit, int size) {
    int range    = (maxLimit - minLimit) / size;
    int[] array = new int[size];
    Random rand = new Random();
    for (int i = 0; i < array.length; i++ ) {
        array[i] = minLimit + rand.nextInt(range) + range * i;
    }
    return array;
}

So, in your case, call the method as:

int randomSortedArray = randomSortedArray(0, 10_000, 1_000);

It's very simple and doesn't require any sorting algorithm. It simply runs a single loop which makes it run in linear time (i.e. it is of time complexity = O(1)).

As a result, you get a randomly generated, "pre-sorted" int[] (int array) in unbelievable time!

Post a comment if you need an explanation of the algorithm (though it's fairly simple).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.