1

I am just looking to change my code so that a random array of a fixed length of 100 integers is generated every time the code is ran rather than just have a pre-set array within the code. I am quite new to this so just need a guide in the right direction, thanks

public class Selectionsort {
    public static void main(String[] args) {
        int[] numbers = {15, 8, 6, 21, 3, 54, 6, 876, 56, 12, 1, 4, 9};
        sort(numbers);
        printArray(numbers);
    }

    public static int[] sort(int[] A) {
        for (int i = 0; i < A.length - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < A.length; j++) {
                if (A[j] < A[minIndex]) {
                    minIndex = j;
                }
            }
            int temp = A[minIndex];
            A[minIndex] = A[i];
            A[i] = temp;
        }
        return A;
    }

    public static void printArray(int[] A) {
        for (int i = 0; i < A.length; i++) {
            System.out.println(A[i]);
        }
    }
}
  • 2
    You already know loops, apparently. So what you need to figure out is how to generate random integers in Java. What happens when you google for "how to generate random integers in Java"? – JB Nizet Nov 15 '19 at 10:26

4 Answers4

2
public class Selectionsort {
    public static void main(String[] args) {
        int[] numbers = new int[100]
        Random random = new Random();

        for (int i = 0; i < numbers.length; i++) {
            numbers[i] = random.nextInt(100);
        }
        sort(numbers);

        printArray(numbers);
    }
}

The above snippet will help you to create a random numbers between 1 to 100 for the array of size 100.

Community
  • 1
  • 1
1

You should look at the Random class. More specifically:

  1. nextInt() to fill it one by one
  2. ints​(long) to get an IntStream of fixed size, which can be easily converted to an array with .toArray()
Hawk
  • 2,042
  • 8
  • 16
1

The below would fill an array of 100 integers with Random numbers from 1-1000

int[] numbers = new int[100];
Random rand = new Random();
for (int i = 0; i < numbers.length; i++) {
    numbers[i] = rand.nextInt(1000);
}

However note that the above code might insert duplicates. If you want to avoid that, using a List in parallel to the array and checking whether the generated value already exists should ensure uniqueness :

int[] numbers = new int[100];
List<Integer> numbersList = new ArrayList<Integer>(numbers.length);
Random rand = new Random();
for (int i = 0; i < numbers.length; i++) {
    int j = rand.nextInt(1000);
    while (numbersList.contains(j)) {
        j = rand.nextInt(1000);
    }
    numbers[i] = j;
    numbersList.add(j);
}

Even though I think it would be wiser to get rid of the array and use just the List...

Community
  • 1
  • 1
nullPointer
  • 4,419
  • 1
  • 15
  • 27
0

You can use Math.random method. This code generates an array of 10 random numbers in the range [50,60] inclusive.

int length = 10, min = 50, max = 60;
int[] arr = IntStream.range(0, length)
        .map(i -> (int) (min + Math.random() * (max - min + 1)))
        .toArray();

System.out.println(Arrays.toString(arr));
// [54, 51, 58, 59, 57, 56, 56, 54, 54, 58]

Full set of 11 random numbers:

Set<Integer> set = new HashSet<>();
while (set.size() < max - min + 1) {
    set.add((int) (min + Math.random() * (max - min + 1)));
}

System.out.println(set);
// [50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60]