Tamara JQ
Tamara JQ

Reputation: 369

How Can I Display a N x N Matrix of Random Numbers in Java?

I'm trying to create a matrix of random double numbers. The matrix must be of size n x n and all numbers must be between 1 and 100. I've been trying to sort it out for ages now and I know it must be something so simple (as it usually is).

Here is my code:

public static void main(String[] args) { 
    PrintRandomGraph(RandomArray(4));
}

private static double[] RandomArray(int n) {  
    double[] randomArray = new double[n];
    double[][] randomMatrix = new double [n][n];

    Random rand = new Random(); 
    rand.setSeed(System.currentTimeMillis()); 
    for (int i = 0; i < n; i++) {

        Integer r = rand.nextInt()% 100; 
        randomArray[i] = Math.abs(r);
        for (int j = 0; j < n; j++) {
            Arrays.fill(randomMatrix, i, i+1, randomArray);
        }  
    }

    return randomArray;  
}

private static void PrintRandomGraph(double[] inputArray) {
    int n = inputArray.length;
    double[] showArray = new double[n];
    double[][] showMatrix = new double [n][n];

    for (int j = 0; j < n; j++) {
        for (int i = 0; i < n; i++) {
            double r = inputArray[i];
            showArray[i] = r; 
            Arrays.fill(showMatrix, i, i+1, showArray);
        }
    }

    System.out.println(Arrays.deepToString(showMatrix));
}

When I run the code I get a random array repeated n times like :

[[63.0, 97.0, 64.0, 75.0], [63.0, 97.0, 64.0, 75.0], [63.0, 97.0, 64.0, 75.0], [63.0, 97.0, 64.0, 75.0]]

I think I need to go back to the top of the for loop and add the new array...? Please help =(

Any help is much appreciated. Thank you.

Upvotes: 2

Views: 22005

Answers (2)

Tamara JQ
Tamara JQ

Reputation: 369

Hi all this provides the answer I was looking for

 private static double[][] RandomArray(int n) {
        double[][] randomMatrix = new double [n][n];
        double[] randomArray = new double [n];
         Random rand = new Random(); 
            rand.setSeed(System.currentTimeMillis()); 

        for (int i = 0; i < n; i++) {   

            for (int j = 0; j < n; j++) {
                Integer r = rand.nextInt()% 100; 
                randomMatrix[i][j] = Math.abs(r);
            }

        }

        return randomMatrix;
    }


public static void main(String[] args){

        //PrintRandomGraph(RandomArray(5));
    System.out.println(Arrays.deepToString(RandomArray(5)));
}

Upvotes: 0

npinti
npinti

Reputation: 52185

Did you try something like this:

private static double[][] RandomArray(int n) {
    double[][] randomMatrix = new double [n][n];

    Random rand = new Random(); 
    rand.setSeed(System.currentTimeMillis()); 
    for (int i = 0; i < n; i++) {     
        for (int j = 0; j < n; j++) {
            Integer r = rand.nextInt()% 100; 
            randomMatrix[i][j] = Math.abs(r);
        }

    }

    return randomMatrix;
}

to Print the graph:

public void printGraph(double[][] array, int n) {
    for (int i = 0; i < n; i++) {     
        for (int j = 0; j < n; j++) {
            System.out.println(array[i][j]);
        } 
    }
}

To Print Graph with Square brackets:

public void printGraph(double[][] array, int n) {
        for (int i = 0; i < n; i++) {  
            System.out.print(" [ ");   
            for (int j = 0; j < n; j++) {
                System.out.print(array[i][j]);
            } 
            //Put println instead of print here to have each row in a new line
            System.out.print(" ]");
        }
    }

Also, this seems a lot like homework, if it is, please tag it like so :)

Upvotes: 3

Related Questions