Вопрос

this is what I have got so far...

 public static void main(String[] args) {
        Random random= new Random();
        Matrix mR = new Matrix(3,3,random.nextDouble()) ;
        System.out.println("Here is a 3x3 matrix with random values " +Arrays.deepToString(mR.getArray()));

    }

The problem is when I print this out, all of the values are the same. What I need is different value in each index. I know this can be done simply by creating an array, assigning it random values, then copying that into the matrix. But I need to do this straight from the matrix mR.

Это было полезно?

Решение

Random.nextDouble returns a double, so in the code above you are calling this constructor:

Matrix(int m, int n, double s) 

Which constructs a m-by-n constant Matrix.

You should just call this static method instead:

public static Matrix random(int m, int n)

E.g.,

Matrix mR = Matrix.random(3, 3);

See the doc for more information.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top