Filling my arrays with random numbers?

I find it hard to put random numbers into arrays in my test class. The code is in java. I cannot do this individually, because in the end I will need to fill arrays with up to 600 values. Here is a test class:

import java.util.Random; public class test { /** * @param args */ public static void main(String[] args) { int size = 1000; int max = 5000; int[] array = new int[size]; int loop = 0; Random generator = new Random(); //Write a loop that generates 1000 integers and //store them in the array using generator.nextInt(max) generator.nextInt(max); //generating one //I need to generate 1000 //So I need some kind of loop that will generate 1000 numbers. for (int i =0; i<1000; i++) { generator.nextInt(max); } /** * After I do this, I'll have the array, array. * Then comes what under this. * THat method is for measuring the time. * System.currentTimeMillis();, * with this, I can collect a time for the start of the method * and one for the end. * Time at the end, minus the time at the start * gets us the running time. */ long result; long startTime = System.currentTimeMillis(); sort.quickSort(array, 100, array.length-1); long endTime = System.currentTimeMillis(); result = endTime-startTime; System.out.println("The quick sort runtime is " + result + " miliseconds"); long result2; long startTime2 = System.currentTimeMillis(); sort.partition(array, 100, array.length-1); long endTime2 = System.currentTimeMillis(); result2 = endTime2 - startTime2; System.out.println("The partition runtime is "+result2 + " miliseconds"); long result3; long startTime3 = System.currentTimeMillis(); sort.bubbleSort(array, 100); long endTime3 = System.currentTimeMillis(); result3 = endTime3-startTime3; System.out.println("The bubble sort runtime is "+result3 + " miliseconds"); long result4; long startTime4 = System.currentTimeMillis(); sort.selectionSort(array, 100); //change the second number to change //the size of an array. long endTime4 = System.currentTimeMillis(); result4 = endTime4-startTime4; System.out.println("The selection sort runtime is "+result4 + " miliseconds"); } } 

I have already passed a test class and another class from which it calls functions, and there are no errors. I just need to somehow put random values ​​in arrays. Any advice would be much appreciated.

If you look at the code, you will see that I have already made a small function to generate the numbers themselves. I just don’t know how to do this so that the numbers get into the array.

+4
source share
2 answers

You just need to change the line inside the loop to assign a random number to the current index of the array.

 array[i] = generator.nextInt(max); 

Check out the Arrays training stream for everything you need to know about creating, initializing, and accessing arrays.

By the way, instead of repeating 1000 your loop condition should probably go up to size .

 for (int i = 0; i < size; i++) 
+8
source

Since 1.8

 Arrays.setAll(array, i -> { return generator.nextInt(max); }); 

This fills your array with random numbers between 0 (inclusive) and max (exclusive).

Further reading: Array Class and IntUnaryOperator Interface

To say and do, I would rather use the approach provided by Bill Lizard, using a loop to initialize each value.

0
source

All Articles