Fill an array with random numbers

后端 未结 13 2217
感动是毒
感动是毒 2020-12-03 11:54

I need to create an array using a constructor, add a method to print the array as a sequence and a method to fill the array with random numbers of the type double.

He

相关标签:
13条回答
  • 2020-12-03 12:19

    You can simply solve it with a for-loop

    private static double[] anArray;
    
    public static void main(String args[]) {
        anArray = new double[10]; // create the Array with 10 slots
        Random rand = new Random(); // create a local variable for Random
        for (int i = 0; i < 10; i++) // create a loop that executes 10 times
        {
            anArray[i] = rand.nextInt(); // each execution through the loop
                                            // generates a new random number and
                                            // stores it in the array at the
                                            // position i of the for-loop
        }
        printArray(); // print the result
    }
    
    private static void printArray() {
        for (int i = 0; i < 10; i++) {
            System.out.println(anArray[i]);
        }
    }
    

    Next time, please use the search of this site, because this is a very common question on this site, and you can find a lot of solutions on google as well.

    0 讨论(0)
  • 2020-12-03 12:22

    I tried to make this as simple as possible in Java. This makes an integer array of 100 variables and fill it with integers between 0 and 10 using only three lines of code. You can easily change the bounds of the random number too!

    int RandNumbers[]=new int[100];
    for (int i=0;i<RandNumbers.length;i++)
        RandNumbers[i]=ThreadLocalRandom.current().nextInt(0,10);
    
    0 讨论(0)
  • 2020-12-03 12:23

    Probably the cleanest way to do it in Java 8:

    private int[] randomIntArray() {
       Random rand = new Random();
       return IntStream.range(0, 23).map(i -> rand.nextInt()).toArray();
    }
    
    0 讨论(0)
  • 2020-12-03 12:23

    You can call the randomFill() method in a loop and fill your array in the main() method like this.

    public static void main(String args[]) {
        for (int i = 0; i < anArray.length; i++) {
            anArray[i] = randomFill();
        }
    }
    

    You would then need to use rand.nextDouble() in your randomFill() method the array to be filled is a double array. The below snippet should help you get random double values to be filled into your array.

    double randomDoubleValue = rand.nextDouble();
    return randomDoubleValue;
    
    0 讨论(0)
  • 2020-12-03 12:24

    Try this:

    public void double randomFill() {
        Random rand = new Random();
        for (int i = 0; i < this.anArray.length(); i++)
            this.anArray[i] = rand.nextInt();
    }
    
    0 讨论(0)
  • 2020-12-03 12:25

    If the randomness is controlled like this there can always generate any number of data points of a given range. If the random method in java is only used, we cannot guarantee that all the numbers will be unique.

    package edu.iu.common;
    
    import java.util.ArrayList;
    import java.util.Random;
    
    public class RandomCentroidLocator {
    
    public static void main(String [] args){
    	
    	int min =0;
    	int max = 10;
    	int numCentroids = 10;
    	ArrayList<Integer> values = randomCentroids(min, max, numCentroids);	
    	for(Integer i : values){
    		System.out.print(i+" ");
    	}
    	
    }
    
    private static boolean unique(ArrayList<Integer> arr, int num, int numCentroids) {
    
    	boolean status = true;
    	int count=0;
    	for(Integer i : arr){
    		if(i==num){
    			count++;
    		}
    	}
    	if(count==1){
    		status = true;
    	}else if(count>1){
    		status =false;
    	}
    	
    
    	return status;
    }
    
    // generate random centroid Ids -> these Ids can be used to retrieve data
    // from the data Points generated
    // simply we are picking up random items from the data points
    // in this case all the random numbers are unique
    // it provides the necessary number of unique and random centroids
    private static ArrayList<Integer> randomCentroids(int min, int max, int numCentroids) {
    
    	Random random = new Random();
    	ArrayList<Integer> values = new ArrayList<Integer>();
    	int num = -1;
    	int count = 0;
    	do {
    		num = random.nextInt(max - min + 1) + min;
    		values.add(num);
    		int index =values.size()-1;
    		if(unique(values, num, numCentroids)){				
    			count++;
    		}else{
    			values.remove(index);
    		}
    		
    	} while (!( count == numCentroids));
    
    	return values;
    
    }
    
    }

    0 讨论(0)
提交回复
热议问题