Note that there are some explanatory texts on larger screens.

plurals
  1. POHow Can I Display a N x N Matrix of Random Numbers in Java?
    text
    copied!<p>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).</p> <p>Here is my code:</p> <pre><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 &lt; n; i++) { Integer r = rand.nextInt()% 100; randomArray[i] = Math.abs(r); for (int j = 0; j &lt; 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 &lt; n; j++) { for (int i = 0; i &lt; n; i++) { double r = inputArray[i]; showArray[i] = r; Arrays.fill(showMatrix, i, i+1, showArray); } } System.out.println(Arrays.deepToString(showMatrix)); } </code></pre> <p>When I run the code I get a random array repeated n times like : </p> <blockquote> <p>[[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]]</p> </blockquote> <p>I think I need to go back to the top of the for loop and add the new array...? Please help =(</p> <p>Any help is much appreciated. Thank you.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload